You are on page 1of 169

Perl

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!!!!

POPULAR PERL TASKS


System Administration jobs Text/Document Processing Web programming Client-Server programming for Internet Database Interface(Oracle, Sybase, Informix, MySQL etc.) GUI programming on X Windows ( with Tk )

POPULAR PERL TASKS


Text/Document Processing Examples: Assembler Development --> perl utility takes input file(assembly language) and produce binary code(machine language). To convert a code from VHDL to Verilog.

CHECKING PERL VERSION

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

RUN INTERACTIVE PERL


$ $ perl -e print hello world\n perl print hello world\n ^d

FIRST PERL SCRIPT


$ perl first.pl #!/usr/bin/perl print hello world\n ;

EXECUTE PERL SCRIPT


$ perl -c first.pl Check first.pl syntax only $ perl -w first.pl 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

PERL ESCAPE SEQUENCES


ESCAPE SEQUENCE \n \t \b \a \cC \e \\ \ MEANING New line Horizontal tab Backspace Alert (Bell) Control C Escape Backslash Double Quote

PERL ESCAPE SEQUENCES ( 2 )


ESCAPE SEQUENCE \l \L \u \U \E MEANING Lowercase next character Lowercase all until \E Uppercase next character Uppercase all until \E Terminate \U or \L

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 ;

BINARY ASSIGNMENT OPERATORS


SYNTAX : $ variable op= value

perl $b = 100 ; $a += 3 ; $b *= $a ; print $a\t$b\n;

AUTOINCREMENT & AUTODECREMENT


SYNTAX : ++variable , variable++ , --variable , variable--

$ perl $a = 10 ; $b = 20 ; ++$a ; # $a has value 11 $c = $b++ ; # $c is 20 , $b is 21 --$c ; # $c is 19

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

HOW TO GET INPUT


name1.pl print Enter your name : ; $name = <STDIN> ; print Enter your surname : ; $surname = <STDIN> ; print Your name is $name and surname is $surname ;

CHOP() & CHOMP()


chop() remove the last character from the variable $x = espresso ; chop($x) ; # $x is now espress chomp() remove the end of record marker (\n) from the variable chop($var = <STDIN>) ; chomp($var = <STDIN> ) ;

CHOP( ) & CHOMP( ) EXAMPLE


name2.pl

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 ELEMENT ACCESS


$array[index] ; element array1.pl @a = (red , green , blue) ; print First element is $a[0] \n; print Second element is $a[1] \n; print third element is $a[2] \n; print Last index is $#a \n; # Array length = $#a + 1

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( )

PUSH( ) & POP( )


push(): Takes a scalar and an array name as parameters It pushes the value of the scalar at the end of the array Syntax: push(@array,variable or array) ; pop(): Takes an array name as a parameter It returns the top or the last element of the array It removes element permanently from the array Syntax: $variable = pop ( @array ) ;

EXAMPLES
$ perl @a = (1..10) ; push(@a,100) ; print @a\n; $ perl @a = (1..10) ; $y = pop ( @a ) ; print $y\n@a\n ;

PUSH( ) AND POP ( ) EXAMPLE


pushpop.pl @a = (1..10) ; print Enter your data to push : ; chop($a = <STDIN>) ; push(@a,$a) ; print Now \@a has value @a \n; $y = pop (@a) ; print \$y has value $y and \@a has value @a \n;

SHIFT( ) & UNSHIFT( )


unshift(): Takes a scalar and an array name as parameters It puts the value of the scalar at the beginning of the array Syntax: unshift(@array,variable or array) ; shift(): Takes an array name as a parameter It returns the first element of the array It removes element permanently from the array Syntax: $variable = shift ( @array ) ;

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;

SHIFT( ) & UNSHIFT( ) EXAMPLE


shift.pl @a = (1..10) ; @b = (a.. z) ; unshift(@a,@b) ; print Now \@a has value @a\n; $x = shift (@a) ; print \$x has value $x and \@a has value @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;

COMMAND LINE ARGUMENT


Special array variable : @ARGV The script name : $0 arg.pl print The script name is $0 \n; print All command line argument are @ARGV \n;

SIMPLE ECHO PROGRAM


$ echo A B C D E F ABCDE F $ perl -e print @ARGV\n A B C D E F A B C D E F

READ INPUT AS ARRAY


$a = <STDIN> ; @a = <STDIN> ; scalar context list context

DETERMINE ARRAY LENGTH


1. Use scalar( ) operator Ex : @a = (one,two,three) ; print scalar( @a ) , \n ; 2. Use $#ArrayName variable ( last index ) Ex : @arr = (athena , apollo , hercules ) ; print $#arr + 1 , \n ;

4 : FLOW CONTROL
if & unless structure while & until & for structure foreach structure last & redo & next structure Logical Operators

TRUE AND FALSE IN PERL


Any string is true except for "" and "0" Any number is true except 0 Any reference is true Any undefined value is false

TRUE AND FALSE EXAMPLE


0 0.00 0.00 10 23-23 False False True True False

IF STRUCTURE
if ( condition ) { statement; } else { statement; }

CHECKING TRUE OR FALSE


$ perl print Enter value : ; chop($val = <STDIN> ); if ( $val ) { print $val is true \n ; } else { print $val is false \n ; }

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 AND UNTIL


while ( condition ) { .. repeat as long as true } until ( condition ) { .. repeat as long as false .. }

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 ; }

LISTING EVEN NUMBER


$ perl foreach $x (1..100) { if ( ( $x % 2 ) == 0 ) { print $x\n; } }

EXPRESSION MODIFIER
statement if condition ; statement unless condition ; statement while condition ; statement until condition ;

EXTRA FLOW CONTROL


COMMAND goto label last last label next next label redo redo label USAGE Jump to named label Break out of innermost loop ( break in C ) Break out of current loop at label Start next iteration of loop ( continue in C ) Start next iteration of loop at label Restart loop without re-evaluating condition Restart loop label without re-evaluating condition

ECHO MESSAGE AGAIN


quit2.pl print Enter your message : ; chop( $mesg = <STDIN> ) ; for( ; ; ) { last if ( $mesg eq quit ) ; print You enter $mesg \n; print Enter your message : ; chop( $mesg = <STDIN> ); }

NEXT AND LAST EXAMPLE


LINE : while ($line = <STDIN>) { last LINE if ( $line eq \n) ; next LINE if ( $line eq #\n ); }

LISTING EVEN NUMBER


$ perl foreach ( 1..100 ) { next if ( $_ % 2 ) ; print $_ \n ; }

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

SHORT CIRCUIT OPERATOR


1. if ( condition ) { statement ; } statement if condition ; condition && statement ; condition and statement ;

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

GETTING INPUT FROM STDIN


1. while ($_ = <STDIN>) { print $_ ; } 2. while (<STDIN>) { print ; } 3. print while (<STDIN>) ;

ANGLE <> OPERATOR


<> get data from files in command line argument or standard input (keyboard) . cat in Perl print while (<>) ;

CAT WITH FILENAME


$ perl print $ARGV : $_ while ( <> ) ;

OPEN( ) AND FILE HANDLE


open(filehandle , filename) ; COMMAND open(FILE,filename) open(FILE,<filename) open(FILE,>filename) write to it open(FILE,>>filename) existing file MEANING Open for reading Open for reading Create file and Append to

DEFAULT FILE HANDLE


NAME STDIN STDOUT STDERR MEANING Standard Input (keyboard) Standard Output (terminal) Standard Error (same as STDOUT)

SIMPLE OPEN PROGRAM


$ perl open(PASSWD,/etc/passwd) ; print while ( <PASSWD> ) ; $ perl print Enter filename : ; chop($name = <STDIN>); open(FILE,$name) ; print while (<FILE> );

ERROR CHECKING WITH DIE


1. unless(open(FILE,filename)) { print Can not open file \n ; } else { .. } 2. unless (open(FILE,filename)) { die Can not open file ; } 3. open(FILE, filename) || die Can not open file ;

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 ;

SIMPLE WRITE PROGRAM


$ perl open(LOG,>log) or die Can not create log file ; print LOG This message go to log file \n; close(LOG) ; $ cat log This message go to log file

COPY FILE IN PERL


copy.pl $source = $ARGV[0] ; $dest = > . $ARGV[1] ; open(INPUT,$source) or die Can not read $source ; open(OUTPUT,$dest) or die Can not create $dest ; while ( <INPUT> ) { print OUTPUT $_ ; } close(INPUT) ; close(OUTPUT) ;

FILE TEST OPERATOR


OPTION -r -w -x -e -z -s -f -d MEANING File or directory is readable File or directory is writable File or directory is executable File or directory exists File exists and has zero size File exists and return size in byte File is a plain file File is a directory

FILE TEST OPERATOR ( 2 )


OPTION -l -b -c -u -g -T -B -M MEANING File is a symbolic link File is a block special file File is a character special file File or directory is setuid File or directory is setgid File is text file File is binary file Modification age in days

GETTING FILE SIZE


$ perl print Enter file name : ; chop($name = <STDIN> ); if ( -f $name ) { $size = -s $name ; print $name has size = $size \n; }

FILE CHECKING EXAMPLE


print Enter file name : ; chop($name = <STDIN>) ; print $name is readable \n if ( -r $name ) ; print $name is writable \n if ( -w $name ) ; print $name is executable \n if ( -x $name ) ; if ( -f $name ) { $size = -s $name ; print $name is file and has size = $size \n ; } elsif ( -d $name ) { print $name is directory \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 ; }

GLOBAL VARIABLE ( CONT. )


$x = 100 ; print Before invoking : \$x = $x \n ; &showvalue ; print After invoking : \$x = $x \n ; ################## sub showvalue { $x = 999 ; print In function : \$x = $x \n; }

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 ; }

PASSING ARRAY PARAMETER


$sum = &add(1..100) ; print Sum is $sum \n; sub add { local($sum) = 0; foreach (@_) { $sum += $_ ; } $sum ; }

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 ; } }

SPACESHIP <=> OPERATOR


sub number { $a <=> $b ; } Sorting list of number from input 1. print sort number <> ; sub number { $a <=> $b ; } ;

2. perl -e print sort { $a <=> $b ; } ;

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 & FGREP & EGREP


grep global/regular expression/print : grep [ option ] OPTION -n number -i Ignore case MEANING Show line

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

^ (START) AND $ (END)


SYNTAX : ^pattern match pattern at start of line pattern$ match pattern at end of line MEANING Match line at beginning of line Match ksh at end of line Match line that have only test Match empty line

EXAMPLE ^line ksh$ ^test$ ^$

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

NEGATED CHARACTER CLASS


SYNTAX : EXAMPLE q[^u] ^[^0-9] [^..] MEANING Match q followed with any character except u Match any line that do not begin with number

DOT : MATCH A CHARACTER


EXAMPLE file. [0-9]. aa.zz dd.mm.yy ^$ MEANING Match file with a character Match a digit with any character Match aa followed with any character and zz Match dd/mm/yy, dd-mm-yy ,dd:mm:yy , etc. Match any line that has 3 characters

| : 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 )

STAR * : ZERO OR MORE


EXAMPLE a* abc* (abc)* .* x.*x [ab]* (a*|b*) MEANING Match nothing , a , aa , aaa, aaaa , etc. Match ab , abc , abcc , abccc , etc. Match nothing , abc , abcabc , abcabcabc , etc. Match any character any number of times Match any character that start and end with x Match nothing,a,b, ab , ba , aba , aab ,bba ,etc. Match nothing ,a , b , aa , bb ,aaa, bbb , aaaa , etc.

PLUS + : ONE OR MORE


EXAMPLE A+ [0-9]+\.[0-9]* .+ (print)+ [^:]+: MEANING Match A , AA , AAA, etc. Match 0. , 0.0 , 9.99 , 1.01 , etc Match at least one character Match print , printprint , etc. Match non-colon characters that follow with colon

? : 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

PERL CHARACTER CLASSES


Name Equivalent Class [0-9] [a-zA-Z0-9_] [ \t\n\r\f] [^0-9] [^a-zA-Z0-9_] [^ \t\n\r\f] Code \d \w \s \D \W \S \b \B

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 >

Q : Define IP Address pattern match Q : What does <HR\s+SIZE\s*=\s*[0-9]+\s*> match ???

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)

EXAMPLE X{1,5} A{0,1} \d{0,} \w{1,} \(\d{3}\)\s+\d{7}

PERL PATTERN MATCHING


SYNTAX : 1. 2. 2. 3. 4. if if if if if ( $var =~ m/pattern/ ) ( $var =~ m#pattern# ) ( $_ =~ m/pattern/ ) ( m/pattern/ ) ( /pattern/ ) { { { { { .. .. .. .... .... } } } } }

TINY GREP PRGORAM


grep1.pl # This program check for pattern http while ( <> ) { if ( m/http/ ) { print ; } }

ANOTHER GREP PROGRAM


grep2.pl die Usage: $0 pattern file\n if ( scalar(@ARGV) != 2 ) ; $pat = $ARGV[0] ; $file = $ARGV[1] ; open(FILE,$file) or die Can not open file $file !!! ; while ( $line = <FILE> ) { if ( $line =~ m/$pat/ ) { print $line } }

MATCHING URL
url1.pl while (<>) { print if (/http:/) ; print if (/ftp:/) ; print if (/mailto:/) ; print if (telnet:/) ; }

IGNORE COMMENT LINE


comment.pl open(FILE,comment) or die Can not open file !!!\n; while ( <FILE> ) { next if ( /^#/ ) ; print ; }

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; }

MATCHING PHONE NUMBER


phone.pl print Enter telephone number : ; chop($phone = <STDIN> ); unless ( $phone =~ m/^\d{7}$/ ) { print $phone is not valid telephone number \n; }

IGNORING CASE WITH /i


/pattern/i : status.pl print Enter your status :; chop($status = <STDIN>) ; if ( $status =~ m/^m/I) { print You are male \n ; } elsif ( $status =~ m/^f/I) { print You are female \n; }

HOW TO IGNORE CASE


Assume that we want to match either y or Y at beginning of line. 1. Use /I syntax if ($input =~ /^y/I ) 2. Use alternation syntax [] if ($input =~ /^[yY]/ ) 3. Use logical or operator if ( $input eq y or $input eq Y )

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;

MATCH GLOBALLY WITH /g


SYNTAX : /pattern/g

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;

STRIP LEADING SPACE


strip.pl open(FILE,white.txt) or die Can not open file !!!\n; while (<FILE>) { s/^\s*// ; print ; } close(FILE) ;

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

USER NAME INFORMATION


account.pl open(PASSWD,/etc/passwd) || die Error reading !!! ; while(<PASSWD>) { @info = split(/:/) ; print \t$info[0] has real name as $info[4] \n; } close(PASSWD) ;

DIRECTORY SEARCH PATH


path.pl $dirpath = $ENV{PATH} ; @dirname = split (/:/ , $dirpath ) ; foreach ( @dirname ) { print $_\n; }

JOIN( )
SYNTAX : join expr , list

$ perl @list = (bat , cat , penguin ) ; $animal = join(:,@list) ; print $animal\n;

PALINDROME WITH JOIN()


palin2.pl print Enter your word : ; chop($word = <STDIN>) ; @forw = split(//,$word ); $back = join(,reverse @forw) ; if ( $word eq $back ) { print $word is palindrome\n ; } else { print $word is not palindrome \n; }

1. /etc/passwd 1. account 0 2. UID 3. account password

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

HASH LITERLAL & VARIABLE


% Hash variable %ENV , %array %day = ( Mon,Monday,Tue,Tuesday) ; access element hash $hash{$key} $day{Mon} Monday

ACCESS COLOR CODE


color.pl %color = ( red => 0X00F , green => 0X0F0 , blue => 0XF00 ) ; # Same as ( red,0x00f,green,0x0f0,blue,0xf00 ) print Enter color name : ; chop($name = <STDIN>) ; print Code for $name is $color{$name} \n ;

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; }

USEFUL ENVIRONMENT VARIABLES


NAME LOGNAME HOME PATH SHELL MEANING Current login name Home directory Directory search path Login shell

KNOWING OS NAME
osname.pl use Config ; $osname = $Config{osname} ; print This machine run $osname as O.S. \n ;

You might also like