home :: technology :: coding :: perlfaq_notes

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 in regular expressions 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.

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

  • Smart match (~~) in perl 5.10 - Compare against an array of regular expressions
  • “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
    
  • unlink $file || die; # This is wrong! You need to use 'or' here
    
  • 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.

  • What’s wrong with sort and how to fix it:

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

  • Use Unicode::Collate to correctly sort unicode.

Last Updated: Fri, 20 Jan 2012

Feedback

Name:

Email or URL:

Comments: