Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008
The Department of Electrical and Computer Engineering 2 Outline What is Perl Variables Operators Flow Control –Conditions Subroutines File Handling IO System Calls Regular Expressions
The Department of Electrical and Computer Engineering 3 What is Perl Scripting language developed in C –Does not compile –Is run by an interpreter #!/bin/perl Useful for processing text files Can be embedded to C/C++
The Department of Electrical and Computer Engineering 4 Variables Scope –Global (our) –Local (my) Type –Scalar $ –Hash % Predefined –$^O –$_ –$!
The Department of Electrical and Computer Engineering 5 Operators Addition (+) Subtraction (–) Multiplication (*) Division (/) Power (**) Modulus (%) Increment/Decrement (++/--) String Concatenation (.)
The Department of Electrical and Computer Engineering 6 Flow Control If, Elsif, Else While –while( ) Do-While –do{ … } while( ); For –for(my $i = 0; $i < 10; ++$i) Foreach –foreach my $var unless or die
The Department of Electrical and Computer Engineering 7 Conditions Numerical –Equal == | Not equal != –Greater > | Greater or equal >= –Less < | Less or equal < String –Equal eq –Not equal ne Complex conditions –and (all conditions must be true) –or (at least one condition must be true) –not (negates an expression)
The Department of Electrical and Computer Engineering 8 Sub Routines Small blocks of code that perform an action. –Factorial –Absolute Value Are user defined –May return a value Syntax –sub (){ Code –} –Parameters are passed my $firstarg = or shift() –Value is returned using return statement return 0
The Department of Electrical and Computer Engineering 9 File Handling Reading and writing is done sequentially using file handles. open(, “ ”) $var = while( ){ –chomp($_) –$_ } close( )
The Department of Electrical and Computer Engineering 10 IO Reading from the standard input –$number = ; Writing to the standard output –print “ ”; print “$variable”; #displays the value of $variable print ‘$variable’; #displays the word $variable Reading from file handle DATA –$number = ; Writing to file handle DATA –print DATA “ ”; –print <<DATA; Any string –DATA
The Department of Electrical and Computer Engineering 11 System Calls mkdir rmdir chdir rename system exec Shell Commands open(FILE, “ls |”);
The Department of Electrical and Computer Engineering 12 Regular Expressions Search a string for a given pattern $var =~ /hello/; is true if $var contains the string hello $num =~ /([0-9]*)(.*)/ –$1 contains a string full of numbers –$2 contains the string after the first pattern of numbers $str =~ s/hello(.*)/$1/; –Replaces the string in the first // for the one in the second // if($_ =~ /$name/i) searches for a the value inside of $name in $_ ignoring the case
The Department of Electrical and Computer Engineering 13 Regular Expressions Characters that must be escaped –/ –. –$ –% –\ –*
The Department of Electrical and Computer Engineering 14 Questions?