Notes from the Perl FAQ

Like the fellow that never read his car’s owner’s manual, I had never read the entire Perl FAQ. I finally did it over the holidays, and these are some notes that I took.

  • Time::Piece – CPAN module that provides object oriented time objects, It also provides date and time addition, subtraction and comparison and date parsing.

    use Time::Piece;
    
    my $t = localtime;
    print "Time is $t\n";
    print "Year is ", $t->year, "\n";
    
  • Text::Autoformat – CPAN module that provides paragraph formatting and case transformations

  • A list has a fixed set of elements, but an array is variable. You can use arrays for list functions, but you can’t use lists with array functions (push(), pop(), shift(), unshift()). <>

  • @array[1] - The sigil is not the variable type. This is actually a slice with a single element. <>

  • List::Util - first() - Similar to grep, but returns the first element where the result from the block is a true value.

    Also includes max(), min(), shuffle(), sum(), and reduce(). The perldoc also includes a number of example subroutines (all, any, none, notall).

  • "\L$a" - Returns the contents of $a, but all lowercase.

  • $| = 1 - perl filehandle select. Each filehandle has its own copy of this value, so you can’t set it globally.
    If you use IO::Handle, you can call the autoflush method to change the setting of the filehandle:

    use IO::Handle;
    open my( $io_fh ), ">", "output.txt";
    $io_fh->autoflush(1);
    

    <>

  • print "@array" puts spaces between the elements, print @array does not.

  • /o, which tells Perl to complie a regular expression only once, is obsolete as of 5.6.

  • use re 'debug'; to debug regular expressions

  • \G in regular expressions - Used with the /g flag, this anchors the last match. It uses the value of pos() as the position to start the next match. It is similar to the string anchor ^ and can be helpful in finding only consecutive matches. <>

  • The special variables @- and @+ replace $```, $&, and $'` <>

  • Smart match (~~) in perl 5.10 - Compare against an array of regular expressions

    my @patterns = ( qr/Fr.d/, qr/B.rn.y/, qr/W.lm./ );
    if( $string ~~ @patterns ) {
    	...
    };
    
  • “A class is just a package, and its methods are just the package’s subroutines”

    See perlboot, perltoot, perlbot, and perlobj

  • Although it has the same precedence as in C, Perl’s ?: operator produces an lvalue. This assigns $x to either $a or $b, depending on the trueness of $maybe:

    ($maybe ? $a : $b) = $x
    
  • Don’t use the double-pipe or with unlink and other commands that are list operators, or put in extra parentheses: <>

    unlink $file || die; # This is wrong! You need to use 'or' here
    (unlink $file) || die; # This will work ok
    
  • Use undef on the left side to skip return values in a list

    my ($name, $address, undef, undef, $zip) = get_address( $person );
    
  • redo - restarts the loop block without evaluating the conditional again.

  • Creating a module - perlmod, perlmodlib, perlmodstyle explain modules in all the gory details.

    perlnewmod gives a brief overview of the process along with a couple of suggestions about style.

    If you don’t need to use C code, other tools such as ExtUtils::ModuleMaker and Module::Starter can help you create a skeleton module distribution.

  • Tom Christainsen’s article on why you almost always need a comparison function when calling sort. Perl’s cmp function (and C’s strcmp) is not an alphanumeric comparator – it is a code point comparator.

http://www.perl.com/pub/2011/08/whats-wrong-with-sort-and-how-to-fix-it.html