Add to library search path
use lib "$PATH_TO_PM_FILES";
Perl -V checks environment
Include/exclude Perl script files
do 'stat.pl' # same as eval `cat stat.pl`
always check $@ for error afterward
do {....} also returns the output of last command
Print all environment defines
Print all installed DBI drivers
my @drivers = DBI->available_drivers;
print join(", ", @drivers), "\n";
$s = scalar value
$s[1] = array element
$s{'k'} = hash element
@a = entire array
%h = hash
(...) = list
{} = array
Pass by Reference
&func (\$arg);
sub func { my ($arg) = @_; my $def = ${$arg}; };
These variables are different
my $var1;
my @var1;
my %var1;
&val1;
filehandle val1
directory handle val1
$var1 = 'val1';
@var1 = ('val1','val2','val3');
%var1 = ('key1','val1','key2','val2')
Difference between a list and an array
- A list is an ordered collection (a, b, c, d)
- An array is a container for a list @a = (a, b, c, d)
- Force a list context with [ ] or ( )[]
- Use @{[ ]} or eval {} to make a copy of a list
Default arguments
@ARGV
$_
@_ : function argument list
$$ : pid
Extract subroutine arguments
Using shift
my $arg1 = shift;
my $arg2 = shift;
Using array
my $arg1 = $_[0];
my $arg2 = $_[1];
Intelligent Equality
Instead of == or eq, try using ~~
%hash1 ~~ %hash2 # the hashes have the same keys
@array1 ~~ @array2 # the arrays are the same
%hash ~~ @keys # one of the elements in @keys is a key in %hash
$scalar ~~ $code_ref # $code_ref->( $scalar ) is true
http://perldoc.perl.org/perlop.html#Smartmatch-Operator
Operator precedent
and, or have the lowest precedency
Boolean
Four FALSE values: undef, '', 0, '0'
All other values are TRUE
Make enhanced features available
use 5.010;
Make specific feature available
use feature qw(say);
The return value of =