department of electrical and computer engineering introduction to perl by hector m lugo-cordero...
TRANSCRIPT
Department of Electrical and Computer Engineering
Introduction to Perl
By Hector M Lugo-CorderoAugust 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 $– Array @– 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(<expr>)
• Do-While– do{ … } while(<expr>);
• For – for(my $i = 0; $i < 10; ++$i)
• Foreach– foreach my $var (@myarr)
• 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 <subroutine_name>(){
• Code
– }– Parameters are passed using @_
• my $firstarg = shift(@_) 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(<FILEHANDLE_NAME>, “<filename>”)
• $var = <FILEHANDLE_NAME>• @array = <FILEHANDLE_NAME>• while(<FILEHANDLE_NAME>){
– chomp($_)– $_
• }• close(<FILEHANDLE_NAME>)
The Department of Electrical and Computer Engineering
10
IO
• Reading from the standard input– $number = <STDIN>;
• Writing to the standard output– print “<MESSAGE>”;
• print “$variable”; #displays the value of $variable• print ‘$variable’; #displays the word $variable
• Reading from file handle DATA– $number = <DATA>;
• Writing to file handle DATA– print DATA “<MESSAGE>”;– 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?