Tutorials
Scalar references:
$ra = $a; # reference to scalar
$$ra = 2; # dereference scalar- ref
$ra = 1.6; # reference to constant scalar

Array references:
$rl = @l; # reference to existing
$rl = [1,2,3]; # reference to anonymous array
push (@$rl, "a"); # Dereference
print $rl->[3] # 4th element of array pointed to by $rl

Hash references:
$rh = %h; # reference to hash
$rh = {"laurel" => "hardy", "romeo" => "juliet"}; # ref to anon-hash
print keys (%$rh); # Dereference
$x = $rh->{"laurel"}; # Arrow notation to extract single element
@slice = @$rh{"laurel","romeo"}; # Hash slice

Code references:
$rs = &foo; # reference to existing subroutine foo
$rs = sub {print "foo"}; # reference to anonymous subroutine
# (remember the semicolon at the end)
&$rs(); # dereference: call the subroutine

Generalized dereferences. Any code inside a block yielding a reference can be dereferenced:
@a = @{foo()}; # dereference the array reference
# returned by foo()
References gotchas. All the examples below are wrong. Always use -w in developing and testing.
@foo = [1,3,4]; # Assigning an array-ref to an array
# Use parentheses instead.
%foo = {"foo" => "bar"}; # Assigning a hash-ref to a hash.
# Use parentheses instead.
$foo = ($a, @b); # Identical to $foo = ($a, @b)
# Assiging an enumerated list to a
# scalar yields the last element (so,
# $foo gets @b). Use [ ] if you need
# an array reference
Lists do not nest like this:
@foo = (1, 3, ("hello", 5), 5.66);

For nesting, make the third element a reference to an anonymous array:
@foo = (1, 3, ["hello", 5], 5.66);

An example of a nested data structure (a hash of array of hashes):
$person = {   # Anon. hash
"name" => "John", # '=>' is an alias for a comma "age" => 23, "children" => [ # Anonymous array of children
{ "name" => "Mike", "age" => 3, }, { "name" => "Patty","age" => 4 } ] };
print $person->{age}; # Print John's age
print $person->{children}->[0]->{age}; # Print Mike's age
print $person->{children}[0]{age}; # Print Mike's age,omitting
# arrows between subscripts


To pretty-print $person above:
use Data::Dumper;
Data::Dumper->Dumper($person);

# Or,

require dumpVar.pl;
main::dumpValue($person);
Exception Handling
die throws an exception, and eval traps it. Error string is found in $@. The following code has the possibility of two run-time errors:
eval {
$c = $a / $b;
die "Denominator cannot be negative" if ($b < 0);
};
print "Run-time error: $@";
  • $@ can be "Illegal division by zero" or "Denominator
  • cannot be negative".
    Meta-Information
    Call-stack information. Use caller() to find out who's calling this subroutine:

    ($package, $file, $line) = caller();

    List of a package's global variables. For a package Foo, %Foo:: contains the symbol table, whose keys are names of global identifiers in that package and whose values are typeglobs.

    Find out what a reference contains. ref($r) returns undef if $r is an ordinary scalar, "SCALAR" if it is a reference to a scalar (similarly "ARRAY," "HASH," "CODE," and "REF") or the name of a package, if $r is a blessed object reference.

    Object information:
    $obj->isa("Foo"); # returns true if $obj inherits from Foo
    $obj->can("bar"); # returns true if it supports method "bar"
  • Perl
    My First Perl Program
    Perl Scalars
    Perl Lists
    Perl Operators
    Perl Arrays
    Perl Hashes
    Perl If..elsif..else
    @ARGV and %ENV
    Perl Loop statements
    Perl Subroutines
    Perl References
    Perl Regular Expressions
    Perl File Operations
    Perl Objects, Classes
    Perl DBI (Databases)
    Perl Signals
    Perl command line..
    Perl Special Variables
    Perl Reference
    Perl SOAP
    Perl Threads