Professional Documents
Culture Documents
Introduction Scalar Data Array Flow Control File I/O Regular Expression Subroutine
INTRODUCTION
PERL : Practical Extraction and Report Language First developed by Larry Wall subsequently by PERL Community Although continuously evolving, current version is 5.005_03 ( Perl 5 ) Supported by Unix, Windows, Macintosh and LINUX Freely available on Internet (www.perl.com) Open Source language - Source Code available on the Net!!!!
perl -v
PERL OPTIONS
OPTION -v -e -c -w MEANING Displaying Perl version Execute perl code directly Check syntax but do not execute Execute and show warning message
2 : SCALAR DATA
Scalar Data Scalar Variable Scalar Operator How to show output How to get input
NUMBER LITERAL
Integer ; Floating Point Integer : 1 , -9000 , 2343 Floating Point : 1.99 , -3.25e10 , 900E200
STRING LITERAL
Unix shell string Single -quoted string hello , $xyz , free format\n Double-quoted string good bye , $xyz , fix format\n
PRINT COMMAND
$ perl print hello world ; print Welcome to Perl\n ; print Enjoy yourself \n
Resul t : hel l o w orl d W el com e t o Perl Enj oy yoursel f \nved4: /ohm /users/ .
ALTERNATIVE QUOTES
Single quote can use q/./ Double quote can use qq/../ $ perl print q/This is single quote/ ; print qq/This is double quote\n/ ; ^D
PRINT EXAMPLE
$
perl print print print print ^D \lFIRST LINE\n; \LCOMPUTER\E \n ; \uthailand\n; \Uindia is great\n; fIRST LINE computer Thailand INDIA IS GREAT
SCALAR VARIABLE
Scalar variable : $ examples: $x $AveryLongVariableName $day_in_month $year2000
NUMERIC OPERATORS
PRECEDENCE ** * / + %
OPERATOR EXAMPLES
$ perl print 10 * 100 , \n; print 100 % 2 , \n; print 2 ** 64 , \n ; perl -e print 100 / 3 , \n ;
ASSIGNMENT OPERATORS
SYNTAX : $ perl $a = $b = $c = print variable = value ;
10 ; 20 ; $a + $b ; $c\n;
# This is comment
# Same as print $a + $b , \n ;
STRING OPERATOR
String Concatenation ( . ) /etc . / . passwd ==> /etc/passwd $b = $c . \n String Repetition Operator ( x ) print * x 10 ; print < x 30 ;
COMPARISON OPERATORS
Numeric == != < <= > >= String eq ne lt le gt ge Meaning Equal Not equal Less than Less than or equal to Greater than Greater than or equal to
print Enter your name : ; $name = <STDIN> ; chop($name) ; print Enter your surname : ; chomp($surname = <STDIN>); print Your name is $name and surname is $surname\n;
3 : ARRAY
Array Literal Array Variable Element access on Array Operations on Array Command line argument
ARRAY LITERAL
array : ( value1, value 2, value 3 , ,value n) or ( value 1 .. value n ) array : ( 1 , 2 , 3 , 4 , 5 ) or ( 1..5 ) (one,two,three) ( $a , $b , $c ) ( Jan , 1 , Feb , 2 , Mar , 3)
ARRAY VARIABLE
Array Variable : @name $ perl @day = (mon,tue,wed,thu,fri,sat,sun) ; print @day\n; @x = (1..100) ; print @x ;
ARRAY ASSIGNMENT
@a = (a..z) ; ( 10 , 20 , 30 ) ; @a ; @a ; @a ; ($y,$x) ; # # # # # $a=10, $b = 20 , $c = 30 copy array $a get length of @a $a get first element of @a Swap $x and $y ($a,$b,$c) = @b = $a = ($a) = ($x,$y) =
@z
= () ;
# Null list
ARRAY SLICE
@array[list] slice.pl @a = ( a..z) ; print @a[0,1,2] \n; # The same as @a[0..2] print @a[0..$#a] \n; # The same as @a @b = @a[0..10] ; print @b\n;
OPERATIONS ON ARRAY
push( ) and pop( ) shift( ) and unshift( ) reverse( ) sort( )
EXAMPLES
$ perl @a = (1..10) ; push(@a,100) ; print @a\n; $ perl @a = (1..10) ; $y = pop ( @a ) ; print $y\n@a\n ;
EXAMPLES
$ perl @a = ( 1..10 ) ; $x = shift ( @a ) ; print $x\n@a\n ; $ perl @a = ( 10..20 ) ; @b = ( 100..105 ) ; unshift( @a, @b ) ; print @a\n;
REVERSE( )
Syntax: reverse(@array) $ perl @a = (a.. z) ; @b = reverse (@a) ; print @b\n; @a = reverse (@a) ; print @a\n;
SORT( )
Syntax: sort(@array) sort1.pl @a = (batman, robin, batgirl, freeze, poison ivy) ; @b = sort (@a ) ; print @b\n; @c = (1,2,3,10,15,25) ; @d = sort (@c ) ; print @c\n;
4 : FLOW CONTROL
if & unless structure while & until & for structure foreach structure last & redo & next structure Logical Operators
IF STRUCTURE
if ( condition ) { statement; } else { statement; }
IF EXAMPLE
if1.pl print Enter your status : ; chop($status = <STDIN>) ; if ( $status eq single) { print Welcome to Bachelor group \n; } else { print Congratulations !!! you are so lucky \n; }
UNLESS STRUCTURE
unless ( condition ) { statement } else { statement }
IF -ELSIF STRUCTURE
if ( condition ) { statement } elsif ( condition ) { statement } else { statement }
WHILE EXAMPLE
sum.pl print Enter number : ; chop($n = <STDIN>) ; $sum = 0 ; while ($i <= $n) { $sum += $i ; ++$i ; } print Sum from 1 to $n = $sum \n;
ECHO MESSAGE
quit.pl print Enter your message : ; chop($mesg = <STDIN> ) ; until ( $mesg eq quit ) { print You entered $mesg \n; print Enter your message : ; chop ($mesg = <STDIN> ) ; }
FOR STRUCTURE
for( initialization ; condition ; expression ) { }
Exam pl e: $ perl f or ( $x = 0 ; $x <= 10 ; ++$x ) { pri nt $x\n; } $ perl f or ( $y = 1 ; $y <= 100 ; $y += 1 ) { pri nt $y \n ; pri nt ********\n ; }
FACTORIAL EXAMPLE
fact.pl if ( $ARGV[0] eq ) { print Usage : $0 number \n ; exit ; } $sum = 1 ; for( $i = 1 ; $i <= $ARGV[0] ; ++$i ) { $sum *= $i; } print Factorial of $ARGV[0] = $sum \n;
FOREACH STRUCTURE
foreach variable ( list ) { statement . }
FOREACH EXAMPLE
1. 2. 3. 4. foreach $x (1..10) { print $x\n ; } foreach (1..10) { print $_\n ; } for (1..10) { print ; print \n ; } foreach ( reverse (1..10) ) { print ; }
EXPRESSION MODIFIER
statement if condition ; statement unless condition ; statement while condition ; statement until condition ;
LOGICAL OPERATORS
EXAMPLE $a && $b $a || $b !$a $a and $b $a or $b not $a MEANING True if both $a and $b are true True if both or either one is true True if $a is false The same as $a && $b The same as $a || $b The same as !$a
2. 3. 4.
ARROW MESSAGE
arrow.pl $mesg = TANSTAAFL ; $num = 5 ; for ( $k = 0 ; $k <= $num ; ++$k ) { $blank = x $k ; print \t$blank$mesg\n; } for ( $k = $num - 1 ; $k >= 0 ; --$k ) { $blank = x $k ; print \t$blank$mesg\n; }
5 : FILE INPUT/OUTPUT
Angle <> Operator open( ) , close( ) and file handle File test operators File manipulation Directory manipulation Directory Handle
CLOSE( )
close(filehandle) ; open(FILE,filename) or die Cant read file ; while(<FILE>) { . } close(FILE) ;
WRITING TO FILE
print filehandle data print This is output \n ; print STDOUT This is output \n; print FILE First line \n ;
6 : SUBROUTINE
Defining subroutine
Global and local variable Passing and accessing parameter Recursive function Sorting in Perl
DEFINING SUBROUTINE
sub subname { statement 1; statement 2 ; } &subname ;
SIMPLE EXAMPLE
$ perl sub X { print Subroutine example\n; } &X ;
SUBROUTINE EXAMPLE
sub myfunc { print Message in subroutine \n; } print Before invoking subroutine \n ; &myfunc ; print After invoking subroutine \n;
RETURNING VALUE
$ perl sub X { $a = 100 ; $a ; } $value = &X ; print $value\n;
FINDING SUM
sub mysum { @number = (1..100) ; foreach (@data) { $sum += $_ ; } $sum ; } $total = &mysum ; print Total = $total \n ;
GLOBAL VARIABLE
$a = 100 ; $b = 200 ; $sum = &add ; print $a plus $b = $sum \n; sub add { $total = $a + $b ; $total ; }
LOCAL VARIABLE
$x = 100 ; print Before invoking : \$x = $x \n; &showvalue ; print After invoking : \$x = $x \n; ########################## sub showvalue { my ($x) = 999 ; print In function : \$x = $x \n ; }
SPECIAL ARRAY : @_
All parameter passing to subroutine are in @_ array First parameter = $_[0] , Second parameter = $_[1] , and so on
PASSING PARAMETER
$value = &multiply(10,20) ; print 10 multiply 20 = $value \n; sub multiply { my ($first) = $_[0] ; my ($second) = $_[1] ; #### or my($first,$second) = @_ ; my ($product) = $first * $second ; $product ; }
FACTORIAL EXAMPLE
fact.pl if ( $ARGV[0] eq ) { die Usage: $0 number \n; } $sum = &fact($ARGV[0]) ; print Factorial of $ARGV[0] = $sum \n; sub fact { my ( $sum ) = 1 ; for ( $I = 1 ; $I <= $_[0] ; ++$I ) { $sum *= $I ; } }
RECURSIVE FACTORIAL
refact.pl die Usage : $0 number \n unless (@ARGV) ; $result = &fact($ARGV[0]) ; print Factorial of $ARGV[0] = $result \n; sub fact { my ($num) = @_ ; if ( $num == 0 or $num == 1 ) { 1 ; } else { $num * &fact( $num - 1) ; } }
FIBONACCI NUMBER
Definition : F(0) = 0 ; F(1) = 1 ; F(n) = F(n-1) + F(n-2) for n > 1 sub Fib { my ($num) = @_ ; if ( $num == 0 ) { 0; } elsif ( $num == 1 ) { 1; } else { &Fib($num - 1) + &Fib($num - 2) ; } }
SORTING
Ascending : a , b , c , . , 1 , 2 , 3 , Descending : Z , Y , X , , 10 , 9 , 8 , ...
SORTING IN PERL
1. @list = <> ; @sortlist = sort @list ; print @sortlist \n; 2. @list = <> ; print sort @list ; 3. perl -e print sort <> ;
ASCII SORT
@data = (1,13,2,15,10,30) ; @newlist = sort @data ; print After sorting : @newlist \n;
SORTING NUMERICALLY
1. 2. 3. 4. Define a sort subroutine 2 elements from list will be assigned to $a and $b Compare $a and $b If $a is less than $b return -1 If $a is equal $b return 0 If $a is larger than $b return 1 5. Use sort routinename list in main program
NUMERICAL SORT
@data = (1,13,2,15,10,30) ; @newdata = sort number @data ; print After sorting : @newdata \n ; sub number { if ( $a < $b ) { -1 ; } elsif ( $a == $b ) { 0 ; } else { 1 ; } }
7 : REGULAR EXPRESSION
Common regexp. meatacharacters Perl pattern matching Modifier and Backreference split( ) and join( )
REGEXP. CONCEPT
A pattern or template to be matched against a string Regular Expression composed of 2 types of characters Normal text character ( Literal ) Special characters or Metacharacter ( Grammar or Rule )
REGEXP. COMMANDS
Unix utilities : ed , grep , awk , sed , lex ,etc. Editors : vi , emacs ,etc. Programming Languages : Perl , Tcl , Python Programming Environment : Delphi , Visual C++
GREP EXAMPLE
$ grep root /etc/passwd $ grep -n sub *.pl $ grep -I from: mbox $ grep -v ksh /etc/passwd
METACHARACTERS
Start and end of line Character Class Matching any character Alternation Quantifiers
EXAMPLE
$ egrep ^chop *.pl $ egrep ksh$ /etc/passwd $ egrep -n ^root $ egrep ^$ * /etc/passwd
CHARACTER CLASS : [ ]
SYNTAX : EXAMPLE gr[ea]y <H[123456]> <H[1-6]> [a-z][0-9] ^[Tt]he [ ] or [ char1 - charN] MEANING Match gray or grey Match <H1> <H2>,<H3>,...,<H6> The same as above Match any lowercase followed with a digit Match the or The at beginning of line
| : ALTERNATIONS
SYNTAX : pattern | pattern | ..
MEANING EXAMPLE grey|gray Match grey or gray gr(e|a)y Match grey or gray (same as above) (First|1st) [Ss]treet Match First Street , First street, 1st Street , 1st street ^(From|To) : Match ^From : or ^To : <(H|h)[1-6]> Match <H[1-6]> or <h[1-6]>
QUANTIFIERS
CHARACTER * + ? MEANING Zero or more of previous haracters One of more of previous character Zero or one of previous character ( Optional )
? : OPTIONAL ITEM
EXAMPLE ab? colou?r July? 30(th)? fo+ba?r [-+]?[0-9]+ MEANING Match a and ab Match color and colour Match Jul and July Match 30 and 30th Match fobr, fobar, foobr , foobar ,etc Match any integer with optional sign
Digit Word character Space character Not Digit Not word Not space Word boundary Non word boundary
EXAMPLE
EXAMPLE [-+]?\d+ the\s+the \d\d:\d\d\s(am|pm) <\w+> MEANING Match an nteger with optional sign Match double the with at least one space Match time Match any word within < and >
PATTERN RANGE { }
SYNTAX : pattern{ min , max } or pattern{ min , } or pattern{ num } MEANING Match X, XX, XXX, XXXX, XXXXX The same as A? The same as \d* The same as \w+ Match telephone number with (code area)
MATCHING URL
url1.pl while (<>) { print if (/http:/) ; print if (/ftp:/) ; print if (/mailto:/) ; print if (telnet:/) ; }
CHECKING INPUT
input.pl for(;;) { print Enter your command : ; chop($ans = <STDIN>) ; last if ( $ans =~ / ^[qQ] / ) ; # Same as ( <STDIN> =~ /^[qQ]/ ) }
CHECKING DIGIT
digit.pl print Enter any digit : ; chop($reply = <STDIN> ) ; if ( $reply =~ m#^[0-9]+$# ) { print $reply is digit \n; } else { print $reply is not digit \n; }
REGEXP. REPLACEMENT
SYNTAX : s/regexp/replacement/
sub1.pl $_ = this is simple string ; s/string/sentence/ ; # Default with $_ print $_ \n; $x = From here to eternity ; $x =~ s/here/there/ ; print $x\n;
sub2.pl $x = Oh! captain my captain ; $x =~ s/captain/CAPTAIN/g ; print $x\n; $_ = To be or not to be ; s/be/**BE***/gi ; print $_\n;
BACKREFERENCE
SYNTAX / (pattern) (pattern) .. \1 \2 ../ / (pattern) (pattern) / { $1 $2 .. }
EXAMPLE
1. /red.blue./ Match red followed with any char. and blue and any char. 2. /red(.)blue\1/ Match red followed with a char. and blue and with same char. 3. /test(.*)prog\1/ Match test followed with any char. and prog and followed with same sequence 4. /(\w+)\s*=\s*\1/ Match foo=foo , hello = hello , etc..
SWAP WORD
$ perl $_ = hello world; s/(\w+)\W+(\w+)/\2 \1/ ; print $_\n;
EXTRACT INFORMATION
$ perl $_ = This is a test ; /(\w+)\W+(\w+)/ ; print $1\n$2\n; $ perl $_ = Another example program ; ($first , $second) = /(\w+)\W+(\w+)/ ; print $first\n$second\n;
SPECIAL VARIABLE
VARIABLE $& $` $ MEANING Match part of string that match regexp. Match part of string before $& Match part of string after $&
EXAMPLE
$ perl $_ = This is test on perl pattern matching ; /p.*l/ ; print $&\n; print $`\n; print $\n;
SPLIT( )
SYNTAX : split ( /pattern/ , variable ) split /pattern/ split
$ perl $_ = this is a test on split ; @char = split ; # The same as split ( /\s+/,$_ ) print @char\n;
EXTRACTING CHARACTER
SYNTAX : split(//,variable)
extract.pl print Enter your word : ; chop($input = <STDIN>); @word = split(//,$input) ; foreach (@word) { print $_\n; }
PALINDROME EXAMPLE
palin1.pl print Enter your word : ; chop($word = <STDIN>) ; @forw = split(//,$word) ; @back = reverse @forw ; for($I = 0 ; $I < scalar(@forw) ; ++$I ) { if ( $forw[$I] ne $back[$I] ) { die $word is not palindrome\n } } print $word is palindrome\n;
HISTOGRAM EXAMPLE
hist.pl open(DATA, hist.dat) or die Can not open file !!!\n; while(<DATA>) { @input = split ; print $input[0]\t ; print * x $input[1] . \n ; close(DATA) ;
FILE /etc/passwd
FORMAT login:encrypted password:uid:gid:comment:home dir.:shell EXAMPLE root:x:0:0:ROOT:/: www:x:999:1:Web server account :/usr/users/www:/bin/ksh lotus:x:1000:5:Kornkwan :/usr/users/lotus:/bin/csh jaew:x:1001:1:Thanita:/usr2/users/jaew:/bin/bash
JOIN( )
SYNTAX : join expr , list
UID
8 : HASH
Hash Literal and variable keys( ) operator values( ) operator each( ) operator exists( ) operator delete( ) operator Special Hash : %ENV
DEFINITION
Hash ( Associative Array ) is an array that can be accessed by key name Each pair in hash is interpreted as key/value pair
KEYS( ) OPERATOR
keys.pl $assoc{name} = Hari Seldon ; $assoc{addres} = Sterling University ; $assoc{city} = Trantor ; @keyname = keys %assoc ; print @keyname \n;
RETRIEVING KEYS
key2.pl %comm = ( ls, dir,mv, rename,rm,del ) ; foreach $key (keys %comm) { print Unix command $key has DOS command as $comm{$key} \n; }
VALUES( ) OPERATOR
value.pl $capital{Thailand} = Bangkok ; $capital{Japan} = Tokyo ; $capital{England} = London ; @capital = values %capital ; print @capital \n;
EACH( ) OPERATOR
each.pl %money = ( Thai => Baht , America => Dollar , Japan => Yen , ); while ( ($country,$name) = each %money) { print $country has money name as $name \n; }
EXISTS( ) OPERATOR
Return true if the specified hash key exists in its hash Example : print Exists \n if ( exists $hash{$key} ) ;
DELETE( ) OPERATOR
delete.pl %bookcover = ( Perl => Camel , Compiler => Dragon , Postscript => Red ); delete $bookcover{Postscript} ; print keys %bookcover ;
ENVIRONMENT VARIABLES
SPECIAL HASH : %ENV
env.pl while ( ($key,$value) = each %ENV ) { print $key has value as $value \n; }
KNOWING OS NAME
osname.pl use Config ; $osname = $Config{osname} ; print This machine run $osname as O.S. \n ;