{"id":566,"date":"2013-07-02T21:12:53","date_gmt":"2013-07-03T04:12:53","guid":{"rendered":"http:\/\/galencharlton.com\/blog\/?p=566"},"modified":"2013-07-02T21:12:53","modified_gmt":"2013-07-03T04:12:53","slug":"tutorial-perl-5-10-and-koha","status":"publish","type":"post","link":"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/","title":{"rendered":"Tutorial: Perl 5.10 and Koha"},"content":{"rendered":"<p>The current stable version of Perl is 5.18.0 &#8230; but for very good reasons, Koha doesn&#8217;t require the latest and greatest.  For a very long time, Koha required a minimum version of 5.8.8.  It wasn&#8217;t until October 2011, nearly four years after Perl 5.10.0 was released, that a patch was pushed setting 5.10.0 as Koha&#8217;s minimum required version.<\/p>\n<p>Why so long?  Since Perl is used by a <strong>ton<\/strong> of core system scripts and utilities, OS packagers are reluctant to push ahead too quickly.  Debian oldstable has 5.10.1 and Debian stable ships with 5.14.2.  Ubuntu tracks Debian in this respect.  RHEL5 ships with Perl 5.8 and won&#8217;t hit EOL until <strong><em>2017<\/em><\/strong>.<\/p>\n<p>RHEL5 takes it too far in my opinion, unless you really need that degree of stasis &#8212; and I&#8217;m personally not convinced that staying that far behind the cutting edge necessarily gives one much more in the way of the security.  Then again, I don&#8217;t work for a bank.  Suffice it to say, if you must run a recent version of Koha on RHEL5, you have your work cut out for you &#8212; compiling Perl from tarball or using something like <a href=\"http:\/\/perlbrew.pl\/\">Perlbrew<\/a> to at least get 5.10 is a good idea.  That will still leave you with rather a lot of modules to install from CPAN.<\/p>\n<p>But since we, as Koha hackers, can count on having Perl 5.10, we can make the most of it.  Here are a few constructs that were added in 5.10 that I find particularly useful for hacking on Koha.<\/p>\n<h3>Defined-OR operator<\/h3>\n<p>The defined-or operator, <code>\/\/<\/code>, returns its left operand unless its value is undefined, in which case it returns the right operand.  It lets you write:<\/p>\n<pre class=\"lang:perl\">\r\nmy $a = get_a_possibly_undefined_value();\r\n$a \/\/= '';\r\nprint \"Label: $a\\n\"; # won't throw a warning if the original value was undefined\r\n<\/pre>\n<p>or<\/p>\n<pre class=\"lang:perl\">\r\nmy $a = get_a_possibly_undefined_value() \/\/ '';\r\n<\/pre>\n<p>rather than<\/p>\n<pre class=\"lang:perl\">\r\nmy $a = get_a_possibly_undefined_value();\r\n$a = '' unless defined($a);\r\n<\/pre>\n<p>or (<a href=\"https:\/\/galencharlton.com\/blog\/2009\/09\/what-is-truth\/\">horrors!<\/a>)<\/p>\n<pre class=\"lang:perl\">\r\nmy $a = get_a_possibly_undefined_value();\r\n$a ||= ''; # if $a started out as 0...\r\n<\/pre>\n<p>Is this just syntactical sugar?  Sure, but since Koha is a database-driven application whose schema has a lot of nullable columns, and since use of the Perl <code>warnings<\/code> pragma is <a href=\"http:\/\/wiki.koha-community.org\/wiki\/Coding_Guidelines#PERL2:_Modern::Perl\">mandated<\/a>, it&#8217;s a handy one.<\/p>\n<h3>Named capture buffers<\/h3>\n<p>This lets you give a name to a regular expression capture group, allowing you to using the name rather than (say) <code>$1<\/code>, <code>$2<\/code>, etc.  For example, you can write<\/p>\n<pre class=\"lang:perl\">\r\nif ($str =~ \/tag=\"(?<tag>[0-9]{3})\"\/ ){\r\n    print $+{tag}, \"\\n\"; # %- is a magic hash that contains the named capture groups' contents\r\n}\r\n<\/pre>\n<p>rather than<\/p>\n<pre class=\"lang:perl\">\r\nif ($str =~ \/tag=\"([0-9]{3})\"\/ ){\r\n    print $1, \"\\n\";\r\n}\r\n<\/pre>\n<p>There&#8217;s a bit of a trade-off with this because the regular expression is now a little more difficult to read.  However, since the code that uses the results can avoid declaring unnecessary temporary variables and is more robust in the face of changes to the number of capture groups in the regex, that trade-off can be worth it.<\/p>\n<h3>UNITCHECK blocks<\/h3>\n<p>The <code>UNITCHECK<\/code> block joins <code>BEGIN<\/code>, <code>END<\/code>, <code>INIT<\/code> and <code>CHECK<\/code> as ways of designating blocks of code to execute during specific points during the compilation process for a Perl module.  <code>UNITCHECK<\/code> code is executed right after the module has been compiled.  In the patch I&#8217;m proposing for <a href=\"http:\/\/bugs.koha-community.org\/bugzilla3\/show_bug.cgi?id=10503\">bug 10503<\/a>, I found this handy to allow module initialization code to make use of functions defined in that same module. <\/p>\n<h3>Warning, warning!<\/h3>\n<p>There are some constructs that were added in Perl 5.10, including the <code>given<\/code>\/<code>when<\/code> keywords and the smart match operator <code>~~<\/code>, that are deprecated as of Perl 5.18.  Consequently, I will say no more about them other than this: don&#8217;t use them!  Maybe the RHEL5 adherents have a point after all. <\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-566\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>More<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\"><span>Tumblr<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>The current stable version of Perl is 5.18.0 &#8230; but for very good reasons, Koha doesn&#8217;t require the latest and greatest. For a very long&#8230;<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-566\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>More<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\"><span>Tumblr<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/galencharlton.com\/blog\/2013\/07\/tutorial-perl-5-10-and-koha\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"Tutorial: Perl 5.10 and Koha http:\/\/wp.me\/p3gJ9y-98 #kohails","jetpack_is_tweetstorm":false},"categories":[10,13,11],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3gJ9y-98","_links":{"self":[{"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/posts\/566"}],"collection":[{"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":24,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/posts\/566\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/galencharlton.com\/blog\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}