perl cheat sheet: http://perldoc.perl.org/perlcheat.pdf ____________________________________________________
Perl types and references
$ $a=10; print $a;
use strict; my $var1; $var1=10; print $var1,"\n";; my $var2 = "5ello"; print $var2, "\n"; my $var1= $var1+ 1; print $var1, "\n"; my $total1 = $var1 + $var2; print $total1, "\n"; my $total2 = $var2 . $var2; print $total2,"\n";
10 5ello 11 16 5ello5ello
@ @a=(0,1,2); print $a[0];
use strict; my @numbs= (0,1,2,3,4,5,6..9); print @numbs,"\n"; my @fruits= qw( qpple orange pear banana ); print join( ", ",@fruits) ,"\n"; my @alpha= ('a'..'z'); print join( ", ",@alpha) ,"\n"; my @alphafruits = (@alpha, @fruits); print join( ", ",@alphafruits) ,"\n"; my $lastletter= $alpha[-1]; # $lastletter =$alpha[$#alpha]; my $firstletter= $alpha[0]; print "lastletter =", $lastletter , " firstletter = " , $firstletter, "\n"; my @subalpha= @alpha[0,2,4,6,8,10,12]; my $asubalpha= $alpha[0,2,4,6,8,10,12]; print '@subalpha = ' , @subalpha, ' $asubalpha ' , $asubalpha , "\n"; my $alphasize = @alpha; print '$alphasize=' , $alphasize ,"\n";
0123456789 qpple, orange, pear, banana a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, qpple, orange, pear, banana lastletter =z firstletter = a @subalpha = acegikm $asubalpha m $alphasize=26
% %a=(ʻaʼ,0); print $a{ʻaʼ};
%currency=( 'uk' => 'pounds', 'usa' => 'dollars' ); %language= qw ( uk english usa american france french);
foreach $country (keys %currency){ print "the currency in ", $country, " is " , $currency{$country} ,"\n" ; }
while (($country,$currencyname) = (each %currency)){ print "the currency in ", $country, " is " , $currencyname ,"\n" ; } the currency in uk is pounds the currency in usa is dollars the currency in uk is pounds the currency in usa is dollars references to a scalar use strict; my $numb=100; my $refnumb = \$numb; print '$numb = ', $numb , ' $refnumb = ' , $refnumb ,"\n" ; print '$$refnumb = ' , $$refnumb ,"\n" ; $$refnumb=10; print '$$refnumb = ' , $$refnumb ,"\n" ;; $numb = 100 $refnumb = SCALAR(0x180b318) $$refnumb = 100 $$refnumb = 10
reference to anonymous array hash
my $fruit_ref = ['apple', 'banana', 'orange']; #reference to a predefined array #my @fruit = ('apple', 'banana', 'orange'); #my $fruit_ref = \@fruit; print "@$fruit_ref " ,"\n" ; print “$fruit_ref->[0]”;
my $hash_ref = {name => 'fred', age => 35}; #my %hash = (name => 'fred', age => 35); #my $hash_ref = \%hash; while(($field, $value) =(each %$hash_ref)){ print $field, "," , $value ,"\n" ; } print "$hash_ref->{age}" ,"\n" ; apple banana orange apple name,fred age,35 35 return a ref from a method sub multiply { ($first, $second)= @_; @stuff=($first *$second, $first ,$second ); return \@stuff; } $refstuffout = multiply(5,10);; print 'The result of ', @$refstuffout[2] , ' multiplied by ' , @$refstuffout[1], ' = ' , @$refstuffout[0] ,"\n" ;
passing around a reference to a subroutine
my $subref = sub { print "callback subroutine\n" }; sub doStuff{ ($reftosub) = @_; &$reftosub(); } doStuff( $subref); callback subroutine
Important variables
$_
while ($_ =
){ print $_ ; } while (){ print ; }
@_
sub callMe { ($first, $second) = @_; print $first, ", ", $second ,"\n" ; } callMe("zahir", "hussain");
@ARGV $ARGV
print "@ARGV" ,"\n"; print "\n"; while (){ print "$ARGV: $_" ; }
$#
@myarray=(1, 2, 3 ,4, 5,6,7,8,9,10,11); print "length=",$#myarray," contents: @myarray" ,"\n" ; $myarray[20]="a"; print "length=",$#myarray," contents: @myarray" ,"\n" ;
Arrays Printing the contents of an array @stuff=(1,"as","de"); print "@stuff \n"; print @stuff, "\n"; $"="-"; print "@stuff \n";
$ perl arrayprint.pl 1 as de 1asde 1-as-de
ARGV example print "@ARGV \n"; while(){ print "$ARGV: $_"; }
$ perl argv.pl testls.pl testls.pl testls.pl: @_=`ls`; testls.pl: foreach (@_){ testls.pl:
print; testls.pl: } testls.pl:
boolean test
$a=10; $b=20; undef $b; $c=0; print "\$a is true\n" if $a; print "\$a is defined \n" if defined($a); print "\$b is true \n" if $b; print "\$b is true \n" if defined($b); print "\$c is true\n" if $c; print "\$c is defined\n" if defined($c);
perl boolean.pl $a is true $a is defined $c is defined
comments
#!/usr/bin/perl -w #single line comment =pod This is a comment So is this =cut "this is just a string"
./comments.pl Useless use of a constant in void context at ./comments.pl line 11.
comparison operator
$a=10; $b=20; $c= $a == $b; # return 1 for true, "" for false $d= $a <=>$b; # return -1 , 0 or 1 print "a=$a, b=$b , c using == = $c , d using <=> = $d \n";
$ perl comparisonoperator.pl a=10, b=20 , c using == = , d using <=> = -1
context
$a=10; $b=20; $c="hello"; if ($c == 0 ){ print "hello evaluates to 0 in numeric context \n"; } if ($c eq "hello"){ print "\$c eq hello \n"; } if ($c == "hello"){ print "\$c == hello in numeric context because both sides evaluate to 0 \n"; } print $a+$b+$c, "\n";
perl context.pl hello evaluates to 0 in numeric context $c eq hello $c == hello in numeric context because both sides evaluate to 0 30