perl cheat sheet a href=httpperldoc.perl.orgperlcheat.pdfa

Reviews
Shared by: armani11
Stats
views:
18
rating:
not rated
reviews:
0
posted:
11/9/2008
language:
English
pages:
0
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

Related docs
The Perl Cheat sheet
Views: 4  |  Downloads: 4
The Perl Cheat sheet
Views: 61  |  Downloads: 11
Perl Pack Unpack Printf Sprintf Cheat Sheet Summary
Views: 1896  |  Downloads: 20
10stripe's Perl Cheat-Sheet
Views: 1  |  Downloads: 1
Lab 1 The Basics of Text Processing in Perl
Views: 0  |  Downloads: 0
Linux Cheat Sheet Contents
Views: 328  |  Downloads: 24
premium docs
Other docs by armani11
Articles of IncorporationCalifornia Simple
Views: 140  |  Downloads: 0
Job requirements checklist
Views: 386  |  Downloads: 20
Form 4972 Tax on Lump-Sum Distributions
Views: 310  |  Downloads: 2
Checklist for purchasing used vehicles
Views: 348  |  Downloads: 9
Certificate of Dissolution
Views: 270  |  Downloads: 3
Form 6251 Alternative Minimum Tax-Individuals
Views: 1140  |  Downloads: 6
SALE OF MOTOR VEHICLE
Views: 694  |  Downloads: 14
Little Essays of Love and Virtue
Views: 331  |  Downloads: 8
Workers Compensation Claims
Views: 422  |  Downloads: 4
Duke Bio 25 Study Questions
Views: 805  |  Downloads: 15
Jon Stewart
Views: 196  |  Downloads: 0
CorpDocs-Board Resolution Suspending an Officer
Views: 222  |  Downloads: 4