Advanced Perl Syntax Cheatsheet

by Ken Ehrman
Last Updated: January 22, 2003
Comments/Requests to: kehrman@digitalbridgeway.com

How-to's:

Download a zip file of several example files, click here (ExampleFiles.zip < 6KB)


# Lists and Arrays (return to top)

#   $x is assigned valn
$x = (val1 ... valn);

#   (val1 ... valn)become elements of @y
@y = (val1 ... valn);

#   $x equals n (where: @y = (val1 ... valn) )
$x = @y;

#   $x equals n-1 (where: @y = (val1 ... valn) )
$x = $#y;

#   $x equals val1 (where: @y = (val1 ... valn) )
($x) = @y;


# List Assignments (return to top)

($var1, ...$varn) = (val1, ...valm);


# List Assignments with Arrays (return to top)

#   $x is assigned val1, and the remaining values become elements of @array.
($x, @array) = (val1 ... valn);

#   (val1 ... valn)become elements of @array, and $x is assigned a null value.
(@array, $x ) = (val1 ... valn);

#   all elements of @array3 and @array4 become elements of @array1, and @array2 becomes an empty array.
(@array1, @array2 ) = (@array3, @array4);


# References and Dereferencing (return to top)

@array = (val1 ... valn);
$x = \@array; # $x is a reference to @array;

#   equivalent ways of accessing val1
$$x[0]; $x->[0];

%hash = (key1, val1... keyn, valn);
$x = \%hash; # $x is a reference to %hash;

#   equivalent ways of accessing val1
$$x{key1}; $x->{key1};

sub doStuff { print "doing stuff"; }
$ x = \&doStuff; # $x is a reference to &doStuff;
#   equivalent ways of accessing val1
&$x; $x->doStuff;


# Anonymous Arrays (return to top)

#   $x is a reference to an anonymous array
$x = [ val1, ... valn ];

#   equivalent ways of accessing the value of the first element in that anonymous array.
$$x[0]; $x->[0];


# Anonymous Hashes (return to top)

#  $x is a reference to an anonymous hash
$x = { key1, val1... keyn, valn };

#   equivalent ways of accessing val1.
$$x{key1}; $x->{key1};


# Anonymous Subroutines (return to top)

#  $x is a reference to an anonymous hash
$x = sub { print "doing stuff"; };

#   calling the anonymous sub.
&$x;


# Multidimensional Arrays (return to top)

#to create an array of references
@refarr = ( \$x, \$y, \$z );
#...and use interpolation to print the values as a list
print ("${$refarr[1]}\n");

# this sets @twoDarr equal to an array of two anonymous arrays
@twoDarr = ( [ 100, 200 ], [ 500, 600 ] );

# here's how to derefrerence
print $twoDarr[0]->[0];  # prints 100
print $twoDarr[0][0];    # also print 100
print $twoDarr[1]->[0];  # prints 500
print $twoDarr[1][0];    # also print 500

# below, notice that $twoDref is a reference to
# an anonymous array of anonymous arrays
$twoDref = [ [100, 200], [500, 600] ];

# here's how to dereference, notice the difference in syntax from above
print $twoDref->[0]->[0]; # prints 100
print $twoDref->[0][0]; # also prints 100
print $$twoDref[0][0]; # again, 100


# Arrays of Hashes (return to top)


# Real array of anonymous hashes
@arrHash = ( { 'first' => 'bob', 'last' => 'jones', 'year' => 'sophomore' }, { 'first' => 'tina', 'last' => 'louise', 'year' => 'junior' } );
print $arrHash[0]{ 'first' }; # prints "bob";

# Anonymous array of anonymous hashes
$arrHashRef = [ { 'first' => 'bob', 'last' => 'jones', 'year' => 'sophomore' }, { 'first' => 'tina', 'last' => 'louise', 'year' => 'junior' } ];
print $arrHashRef->[0]{ 'first' }; # prints "bob";
print $$arrHashRef[0]{ 'first' }; # also prints "bob";


# Hashes of Arrays (return to top)

# Real hash of anonymous arrays
%hashArray = ( 'Beatles' => ["Taxman", "I am the Walrus", "Let it Be" ],
'Rolling Stones' => [ "Satisfaction", "Cloud Nine", "19th Nervous Breakdown" ] );
print $hashArray{ 'Rolling Stones' }->[1]; # prints Cloud Nine

# Anonymous hash of anonymous arrays
$hashArrayRef = { 'Beatles' => ["Taxman", "I am the Walrus", "Let it Be" ],
'Rolling Stones' => [ "Satisfaction", "Cloud Nine", "19th Nervous Breakdown" ] };

print $hashArrayRef->{ 'Rolling Stones' }->[1]; # prints Cloud Nine
print $hashArrayRef->{ 'Rolling Stones' }[1]; # prints Cloud Nine
print $$hashArrayRef{ 'Rolling Stones' }->[1]; # prints Cloud Nine
print $$hashArrayRef{ 'Rolling Stones' }[1]; # prints Cloud Nine

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

there's nothing all that interesting down here... ;-)