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- The sigil is *not* the variable type. This is actually a slice with a single element.
first()- Similar to grep, but returns the first element where the result from the block is a true value.
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 @arraydoes not.
/oin regular expressions is obsolete as of 5.6
use re 'debug'to debug regular expressions
\Gin regular expressions - Used with the
/gflag, this anchors the last match. It uses the value of
pos()as the position to start the next match.
The special variables
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”
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.
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
Module::Startercan help you create a skeleton module distribution.
What’s wrong with sort and how to fix it:
Unicode::Collateto correctly sort unicode.
Last Updated: Fri, 20 Jan 2012