Exploring Koha with REPLs
A REPL is an interactive shell for programming languages that:
- Reads expressions,
- Evaluates them,
- Prints the result and
- Loops back to do it all over again
REPLs are simple tools that I’ve found tremendously useful for learning new languages and exploring unfamiliar code.
REPLs in Perl
Although REPLs are commonplace in the broader programming world, it wasn’t until around 2007 that Perl got a proper REPL of its own. (This is purely speculation, but I believe that it wasn’t until people saw the utility of Rails’ script/console that the Perl community felt compelled to develop a good REPL.) However, in true Perl fashion, there’s more than one way to do it. I know of at least 4 implementations on CPAN and there may be more.
| Module | Executable |
|---|---|
| Shell::Perl | pirl |
| Devel::REPL | re.pl |
| App::REPL | iperl |
| Perl::Shell | perlthon |
Exploring Koha with a REPL
To get a feel for how to use a REPL in conjunction with the Koha codebase, I’ve provided a few examples using Shell::Perl.
To start up Shell::Perl, go to where you have koha installed and run `pirl`.
$ cd ~/koha/dev
$ pirl
pirl @>
Now let’s load up a few modules and explore. First let’s load up C4::Context and get a list of all the userids in the system.
pirl @> use C4::Context;
()
pirl @> $dbh->selectall_arrayref('SELECT userid FROM borrowers');
[
["23529000591678"],
["23529000603242"],
["23529000651225"],
["23529000662701"],
["23529000686353"],
["23529000695412"],
["23529000711078"],
["23529000714163"],
["23529000719519"],
["23529000774373"],
["23529000809559"],
["23529000878885"],
["23529000991266"],
["23529001000463"],
["23529001203323"],
["23529001223636"],
]
Next, let’s use GetMember() from C4::Members to get some information about one of these members.
pirl @> use C4::Members
()
pirl @> $member = GetMember(23529001223636, 'userid');
{
B_address => undef,
B_city => undef,
B_email => undef,
B_phone => undef,
B_streetnumber => undef,
B_streettype => undef,
B_zipcode => undef,
address => "5171 Library Rd.",
address2 => undef,
altcontactaddress1 => undef,
altcontactaddress2 => undef,
altcontactaddress3 => undef,
altcontactfirstname => undef,
altcontactphone => undef,
altcontactsurname => undef,
altcontactzipcode => undef,
borrowernotes => undef,
borrowernumber => 50,
branchcode => "SPL",
cardnumber => "23529001223636",
category_type => "A",
categorycode => "PT",
city => "Portland, ME",
contactfirstname => undef,
contactname => "",
contactnote => undef,
contacttitle => undef,
dateenrolled => "1984-05-09",
dateexpiry => "2020-12-31",
dateofbirth => "1951-06-14",
debarred => undef,
description => "Patron",
email => undef,
emailpro => undef,
ethnicity => undef,
ethnotes => undef,
fax => undef,
firstname => "Tommy",
flags => undef,
gonenoaddress => undef,
guarantorid => undef,
initials => "",
lost => undef,
mobile => undef,
opacnote => undef,
othernames => undef,
password => "42b29d0771f3b7ef",
phone => "(212) 555-1212",
phonepro => undef,
relationship => undef,
sex => "F",
smsalertnumber => undef,
sort1 => "0.91641750830823",
sort2 => "0.27291712562825",
streetnumber => undef,
streettype => undef,
surname => "Peters",
title => undef,
userid => "23529001223636",
zipcode => 44236,
}
pirl @>
This lets you see what GetMember() returns without having to write a throwaway script or adding debugging output to a CGI script.
Another thing that I like to do is use Class::Inspector to see what methods a class supports.
pirl @> use Class::Inspector
()
pirl @> $c = 'Class::Inspector'
"Class::Inspector"
pirl @> use C4::Calendar
()
pirl @> $c->methods('C4::Calendar', 'full')
[
"C4::Calendar::Date_to_Days",
"C4::Calendar::_init",
"C4::Calendar::addDate",
"C4::Calendar::carp",
"C4::Calendar::confess",
"C4::Calendar::croak",
"C4::Calendar::daysBetween",
"C4::Calendar::delete_holiday",
"C4::Calendar::get_day_month_holidays",
"C4::Calendar::get_exception_holidays",
"C4::Calendar::get_single_holidays",
"C4::Calendar::get_week_days_holidays",
"C4::Calendar::insert_day_month_holiday",
"C4::Calendar::insert_exception_holiday",
"C4::Calendar::insert_single_holiday",
"C4::Calendar::insert_week_day_holiday",
"C4::Calendar::isHoliday",
"C4::Calendar::new",
]
pirl @>
Hopefully, this gives you a feel for how you can use REPLs to explore your code.
