Perl
Variables
Scalar $ Array @ Hash %
| my $no | = 132; | # integer |
| $str | = 'Hallo'; | # string - single quotes |
| my @nos | = (1,2,3,4); | # integers |
| @strs | ( 'apples', 'pears', 'plums'); | # fruity strings |
| my %address | =('street' => 'Mohnweg', | |
| 'town' => 'Suerth' ); | ||
| a list is | (1,2,'fred',$a) | a seperated set of things in ( ) |
| Scalars $ | ||
| $x | = 'baa'; | |
| ='$xx'; | # is string $xx | |
| ="$no"; | # is 132 -- interpolation | |
| $y | = $x . $x; | # concatenation => baabaa |
| $w | =undef; | |
| $sc | ="Hallo Readers"; | #notice plural |
| $n | =length($sc); | # 13 number of chars |
| $z | = chop ($x); | # ba |
| $z | = chomp $x); | # bad example as it will only chop |
| last char if it is special ie linefeed | ||
| $/ = \n | ||
| $a | = 'dog'; | |
| $letterD | = substr ($a, 0, 1); | |
| case functions | uc,lc ucfirst, lcfirst | |
| Arrays @ | ||
| @ar | =(1..9); | |
| @arr[3,4,5] | =(6,8,10); | # slice |
| $NumberChars | = scalar( @arr ); | # also just @arr or $#arr+1 |
| push (@arr, 'elem'); | #add to end | |
| push (@arr, @arr); | # doubles array | |
| $end | =pop ( @arr ); | # takes from end |
| unshift( @arr, 'begin'); | # adds to beginning | |
| $a | =shift ( @arr ); | #takes from beginning |
| split (REG, $scalar, $limit); | # splits scalar dependent on REG | |
| for REG =''; # empty | splits int chars | |
| for REG=' '; # space | splits into words | |
| join ('seperator' , @arr); | # array to scalar | |
| splice | # look them up | |
| reverse | ||
| Hash % | ( looses order) | |
| $bag{'green'} | = 'Sweets'; | |
| %hsh | =(k1,v1,k2,v2); | #use of list |
| %hsh1 | =(k1=>v2, k2 => v2); | |
| $val | = $hsh(k1); | |
| @arr | = keys( %hsh ); | |
| @arr | = values ( %hsh ); | |
| foreach $k (keys %hsh) | ||
| while ( ($k,$v) = each %hsh ) | #each is a function | |
| delete $ENV {PATH}; | # one element | |
| undef (%hsh1 ); | #deletes everything | |
| if ( exists ($h{'key'}) ) | ||
| Handles | ||
| open ( FD, "ascii.dat"); | ||
| $line | =<FD>; | |
| close (FD); | # often left to perl | |
| open (FD, | "input.file); | # default is read |
| open (FD, | "> file" ); | # write to file |
| open (FD, | ">> file" ); | # append |
| open (FD, | "process |" | #pipe from process to here |
| open (FD, | "| process"); | #pipe to process |
| OOP version | ||
| my $fh | = new filehandle ("file" ); || die; | |
| $line | =<$fh>; | |
| fh->close(); | ||
| @lines = <FD>; | # read whole file | |
| while ( $line = <FD>){ | # read line by line | |
| print $line; } | ||
| @arr | = <$fh>; | #whole file int array |
| local ($/) | = undef; | |
| $sc | <$fh>; | # whole file into scalar |
| Control Structures | ||
| while ($x<10){ | ||
| print $x; } | ||
| continue{ | ||
| print 'something'; } | ||
| next | ||
| last | ||
| Subroutines | ||
| sub f1 { | ||
| my ($a1,a2) | = @_; | # or $a1 = shift or $_[0] |
| ----- | # or $a2 = shift or $_[1] | |
| return ( $f ); | ||
| wantedarray() | ? return @arr | : return $scalar; |
| my ( @arr ) | = @_; | # common form |
| f1( \@ar1, \@ar2); | # references | |
| sub f1 { | ||
| my ( $ar1, $ar2) | = @-; | |
| References | ||
| $ref | = \@arr; | |
| @$ref | =(1,2,3); | |
| my $a | = [1,2,3,4]; | |
| my $d2 | =([1,2], [3,4], [5,6] ); | |
| $h | ={'one' => 12 , 'two' => 9 }; | |
| Dereferencing | ||
| $r | = \'a scalar'; | |
| print $r; | # ARRAYREF(0x12123) | |
| #memory location | ||
| print $$r; | # a normal scalar | |
| $hr | = {k1 => v2}; | |
| print $hr->{k1}; | # v2 | |
| # normal hash is $h{k1} | ||
| $li | = [0,3,2]; | |
| $a | =$li->[1]; | #3 |
| $b | =$$li[1]; | #3 |
| $cr | =[1,2,3, [4,5,6] ]; | |
| print "@{$cr->[3]}" | # 4,5,6 | |
| Regular Expressions | ||
| $s | =quotemeta( '({' ); | # \(\{ |
| Namespace | ||
| $name::xx++; | ||
| $name::f1($name::xx); | ||
| or | ||
| package name | ||
| $xx++; | ||
| f1($xx); | ||
| Libraries and require | ||
| Store subroutines in name.pl | ||
| then to use | ||
| require "name.pl"; | ||
| f1($xx); | # all done at run time | |
| Modules and use | ||
| module in name.pm | ||
| package name; | ||
| --- | ||
| and used as in | ||
| use name; | ||
| name::f1($x); | ||
| or | ||
| module in name/christian.pm | ||
| package name::christian | ||
| ----- | ||
| and used | ||
| use name::christian | ||
| name::christian::f2($x); | ||
| module in add.pm | ||
| package add; | ||
| sub import | #special proc | |
| my ($t,$p) | =@_; | |
| --- | ||
| activated as in | ||
| use add(2,2); | #initialisation | |
| module name.pm | ||
| package name; | ||
| use exporter; | ||
| @ISA | =qw(Exporter); | #just learn for now |
| @EXPORT | =qw (True ); | |
| then name::f2($x) can be just f2($x) | ||
| Objects | ||
| package name; | ||
| sub new { | ||
| my ($type, $arg) | =@_; | |
| my ($self) | ={}; | |
| $self->{'name'} | $arg; | |
| bless $self, $type; | ||
| } | ||
| sub destroy{ | ||
| } | ||
| Package Complex; | ||
| use overload "+" => \+ | ||
| sub plus{ | ||
| my ($c1,$c2) | =@_; | |
| return ( complex->new( $c1->{real} +$c2->{real}, $c1->{im} +$c2->{im} |
||
| } | ||