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

for (keys(%ENV)) {
    print $_, " = ", $ENV{$_}, "\n";
}
 
or
 
map { say $_, ' -> ', $ENV{$_} } keys(%ENV);
 
Print all include paths
 
map { say $_ } @INC
 
Print all global variables in current symbol table
 
map { say $_, ' -> ', $::{$_} } keys(%::); 

Print all installed DBI drivers

my @drivers = DBI->available_drivers;
print join(", ", @drivers), "\n"; 

Primitives Types

$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 =

The return value is the number of elements on the right side, thus these two are possible
 
my $count =()= m/(...)/g;
my $count =()= split /:/, $line;
 
The => operator
 
key => value can be used as a key-value pair in a hash
key is always interpreted as a string and not a function
 
Function hint
 
Call a function using
 
func() or &func or +func
 
If &func is used and no () for parameters, current @_ get passed by default
 
Auto die and exception
 
Use autodie and catch exception with veal {}
 
Use AUTOLOAD to catch call to undefined subroutines
 
sub AUTOLOAD {
say "$AUTOLOAD NOT IMPLEMENTED";
}