Perl% - OS3 · PDF filepresent, and Perl 6 is the future. ... A%simple%program%to%startwith:%...
-
Upload
trannguyet -
Category
Documents
-
view
216 -
download
2
Transcript of Perl% - OS3 · PDF filepresent, and Perl 6 is the future. ... A%simple%program%to%startwith:%...
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, . . . 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
IntroducEon
• “PracEcal ExtracEon and Report Language” • Created as a way to make report processing easier. • Developed by Larry Wall, as system administrator at NASA, in the late 1980s
• It's an interpreted language, but can be compiled as well
• Is now being used for: – System administra5on automa5on – Glue between systems, conversion – CGI backend for websites – Much more. . .
Things to know about perl
• Perl vocabulary is extremely rich and its grammar is very flexible
– Perl programmers usually say "There's more than one way to do it".
– Because of this, some Perl scripts can be very difficult to read. WriEng them though, is always a pleasure.
• In fact, you can literally write a Perl script your way, with your own style. – Some people even do "poetry" in Perl
Clement Lefebvre: h`p://www.linuxforums.org/arEcles/learn-‐perl-‐in-‐10-‐easy-‐lessons-‐lesson-‐1_120.html
Perl documentaEon
• Every release of Perl comes with documentaEon in a set of files. – Most releases have over 1,700 pages of documentaEon included in reference books, user guides, FAQs, and so on.
• On most operaEng systems, a uElity called perldoc is installed as part of the Perl system. – The perldoc uElity can search for and format Perl documentaEon for you.
– To use perldoc to look up the basic syntax for perl, open a terminal or console and issue the command: perldoc perl
More on perldoc • The Perl documentaEon is divided into parts by purpose:
– perlfunc (Perl funcEons) – perlfaq (Perl FAQs) – perlop (Perl operators)
• To search for a parEcular keyword, use the –k opEons. For example to look up the print keyword: perldoc –tf print
• To search the FAQs use –q as an opEon: perldoc –q perl
What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future.
h`p://perldoc.perl.org/index-‐language.html
In term of Versions
• 1987 – Perl 1, the start … • 1988 – Perl 2, featured a be`er regular expression engine • 1989 – Perl 3, added support for binary streams • 1993 – Perl 4, a series of maintenance releases • 1994 – Perl 5, major modificaEon: including objects, references, lexical (my) variables, and modules
• 2000 – Perl 6, “Perl 5 and Perl 6 differ fundamentally” (h`p://en.wikipedia.org/wiki/Perl_6)
Perl interpreter • Perl is an interpreted language.
– h`p://stackoverflow.com/quesEons/5376559/is-‐perl-‐a-‐compiled-‐or-‐an-‐interpreted-‐programming-‐language
• This means that the code you will write using the Perl language will need a program called the Perl interpreter in order to be run.
• For instance, if you write a Perl script in a file called myScript.pl (.pl is commonly used as an extension for Perl scripts), there are two ways to run a perl script. call the interpreter to run it: $ perl myScript.pl
Clement Lefebvre: h`p://www.linuxforums.org/arEcles/learn-‐perl-‐in-‐10-‐easy-‐lessons-‐lesson-‐1_120.html
Perl interpreter: debugger
• Part of the Perl interpreter is a debugger that you can use to examine the execuEon of your Perl scripts.
• The debugger is built into every Perl interpreter and is acEvated with the –d opEon when launching the interpreter:
perl –d myprog.txt
Using strict
• Perl has a keyword called strict.
• Using strict is a good way to enhance your programming abiliEes. To do this, simply put: use strict;
at the top of your code.
Simple Perl
A simple program to start with: #!/usr/bin/perl
# This is a sample of Perl code # and this how you write comments in Perl
print "Hi there!\n"; print "This is a very";
print "\tsimple program.\n";
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, subrouEnes 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
Variables
• Three different types of variables: scalars, arrays and hashes
• Start with the dollar (‘$’), at (‘@’) or (‘%’) depending on the type of variable $a = 2; $message = "Hello World!"; @colors = ("red", "green", "blue"); @primaryNumbers = (1, 2, 3, 5, 7); %phoneNumbers = (Alicia => "090-64-773315", Tom => "085-153-3214", Jimmy => "085-285-4545");
• Variable names are a punctua5on character, a leBer or underscore, and one or more alphanumeric characters or underscores .
Declaring variables
• Unlike many programming languages, variables do not need to be declared prior to use with Perl. – When the variable is assigned an iniEal value, Perl can figure out the data type.
• If you try to use an uninitalized variable, Perl will – use the value zero for a numeric, – or Null for a string.
• .
Variables: Scalars
• Start with the dollar (‘$’) sign. • Can be a number or a string or an object reference
• Perl automa5cally converts between numbers and strings. $n = 8 + "2” à $n holds "10” $n = 8 . "2" à $n holds "82”
Note: concatenaEon of strings using a period `.’
Variable: Scalars (Strings)
• in double quoted (‘ “ ’) strings escape sequences represent special characters: \n, \t, . . .
• For verba5m strings use single quotes (‘ ‘ ’)
Perl: print "Hi there! \n \t It's me.\n"; print 'And me \n \t as well!';
• output: Hi there! It's me. And me \n \t as well!
The q and qq operators
• Perl allows you to use these structures: – q( ) – qq( ) Instead of single and double quotes, respecEvely. So, qq(This is a test) is the same as “This is a test”
• These can be handy when embedding marks that would otherwise need escaping: qq(He said “Help!” then “Now!”)
Variable Scalars (Numbers)
• simple numbers as per usual: – decimal: 12, -‐17, 255 . . . – octal, start with 0: 015, -‐023, 0777 . . . – hexadecimal, start with 0x: 0xc, -‐0x11, 0XfF, . . . either case allowed for the x or hex digits abcdef.
FloaEng point numbers: – “one and a quarter": 1.25 – “7.25 Emes 10 to the 45th power": 7.25e45. – “negaEve 12 Emes 10 to the -‐24th": -‐12e-‐24.
Variables: Array (1)
• Start with the at (`@') sign. • Arrays represent ordered lists of scalars. • DefiniEon using parenthesized, comma delimited lists: @array_numbers = ( 1, 2, 4, 8, 16 ); @array_strings = ( "one", "two", "3" );
• The index of an array starts at 0, not 1 • Processing single items as a scalar using `[' and `]':
print $array_strings[1]; # prints "two" $array_strings[1] = "2"; # we changed @array_strings
Variables: Array (2)
• Arrays can be created using scalar assignment: $names[0] = "Eelco"; à creates @names to hold ("Eelco”)
• The length of an array: $#array_name, returns the number of items in the array minus one. – If equal to -‐1, the array does not exist or is empty. print $#array_strings; à prints 2
– Assignment to $#array_strings changes the size of the array. $#array_strings = 0;
à@array_strings now holds ( "one" )
Variables: Hash (1)
• Start with the percentage (`%') sign. • Made up of keys and values. • Each key has exactly one corresponding value. • DefiniEon using parenthesized, comma delimited lists of keys and values: %numbers = ("one" => "one", "two" => 2);
• Processing a single hash as a scalar using `{' and `}': print $numbers{"two"}; à prints "2" $numbers{"one"} = "1”; à Change %numbers
Variables: Array Hash (2)
• Like arrays, hashes can be created using scalar assignment:
$strings{"1"} = "one"; à creates %strings to hold ( "1" => "one" )
• keys <hash_name> funcEon will return an array filled with all the keys in the hash.
print keys %numbers; à prints ( "one", "two" )
Variables: $_
• The $_ variable is used by Perl as a default variable. • You can use it in place of variable names in your scripts: $_=“This is a test”; print;
• Use the default operator carefully as it can easily be confusing.
Standard operators
• Perl supports the standard mathemaEcal operators +, -‐, *, /, % (modulus) and ** (exponent)…
• Operator order of precedence applies according to standard rules; parentheses group operaEons
• Operators can be combined in statements: $num1=$num2 * ((3 + 8)*$num3/2);
• MulEple assignments can be made on one line: $num1=$num2=$num3=7;
Boolean operators
• Perl supports to Boolean AND and OR operators in the same way as other high-‐level languages: && AND || OR
• These operators are o�en used for condiEons: if (($num1 < 10) && ($num1 > 5))
• Perl also allows the use of the words “and” and “or”: if ($num1 < 10 and $num1 > 5)
String operators (relaEonships)
• There are relaEonal operators for strings: eq equal to ne not equal to gt greater than lt less than ge greater than or equal to le less than or equal to
• Comparisons are le�-‐to-‐right, using ASCII values
Your second Perl script, the Calculator! #!/usr/bin/perl $nbArguments = $#ARGV + 1; print "number of arguments: $nbArgumentsn"; exit(1) unless $nbArguments == 3; $a = $ARGV[0]; $b = $ARGV[2]; $operation = $ARGV[1]; if ($operation eq "+") { $result = $a + $b;} elsif ($operation eq "-") { $result = $a - $b;} elsif ($operation eq "/") { $result = $a / $b;} elsif ($operation eq "x") { $result = $a * $b;} print "$a $operation $b = $result"; Make the script executable: $ chmod a+rx calculator.pl Run the script : $ ./calculator.pl 5 + 6./calculator.pl 11 - 2./calculator.pl 4 x 3./
calculator.pl 33 / 3
The command line arguments are stored in array @ARGV -‐ first element $ARGV[0] -‐ second element $ARGV[1]... etc. -‐ the index of the last element in the array can be obtained by $#ARGV.
Two operators for comparing scalar variables == and eq Unless is used as a control statement
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, control statements, subrouEne
3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
Code blocks
• A block of code contain a list of statements.
• Code blocks are delimited by `{' and `}’
• Each code block is treated as a unit by Perl, although execuEon is sEll always top to bo`om unless moderated by control structures
• Usually blocks will be associated with other statements, such as if condiEons.
Comparison operators
• Perl supports the standard comparison operators:
> greater than < less than >= greater than or equal to <= less than or equal to == exactly equal to != not equal to
True and false • In Perl, any condiEon that evaluate to false is assigned a value of zero. Anything that is non-‐zero is true.
• This applies to condi5ons in statements (such as the if ) as well as for numeric evaluaEon:
0 false 3 true (non-‐zero) 5-‐5 false (evaluates to zero) 0.00 false (it’s zero with precision) “” Null string is false “ ” String with a space or anything not null is true
Control Statements: CondiEons (1)
• CondiEonal statements: if ( test ) { . . . } elsif { . . . }
else { . . . } unless ( test ) { . . . } else { . . . } while ( test ) { . . . } until ( test ) { . . . }
• Note: comparison of strings using `eq'. 0xa == " 10 " à true
0xa eq " 10 " à false
Control Statements: CondiEons (2) if ($a == 5) { print "It's five!\n";
} elsif ($a == 6) { print "It's six!\n";
} else { print "It's something else.\n";
} unless ($pie eq 'apple') { print "Ew, I don't like $pie flavored pie.\n";
} else { print "Apple! My favorite!\n";
}
Control Statements: loops (3)
$a = 0; while ($a != 3) { $a++;
print "Counting up to $a...\n";
}
until ($a == 0) { $a--;
print "Counting down to $a...\n";
}
For loop
• For loops are for repeEEon • Using a list and an index variable a block of code will be executed for each value in the list. for $i ( 1, 2, 3, 4 ) { . . . }
• Using `..' you can also give a range: for $i ( 1 .. 3, 4 ) { . . . }
• The list can contain any scalars or other lists. @range = ( 1 .. 3 ); for $i ( @range, 4 ) { . . . }
The exit statement
• The exit statement is used to terminate a Perl script at any Eme.
• You can send a return status code back to the calling program with exit, if you want, by appending the return code a�er the exit statement: if ($val1 == 0) { exit 0;}
– This will exit the program with a return code of 0 is $val1 is zero.
Control Statements: next & last
• The next statement causes execuEon of a loop to restart. – It is similar to the conEnue statement in some languages. Any statements below the next statement are not executed.
• The last statement can be used to terminate a loop, regardless of the condiEon’s value. – The last statement is similar to break in other programming languages
Labels
• Blocks of code and the for and while statements can all be labeled, and idenEfied by that label. – To use a label, place it before the statement block followed by a colon: BLOCK1: {statements…}
• The name of the label can be any valid ASCII characters. – The convenEon is to use uppercase for labels so there is no conflict with exisEng keywords.
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, control statements, subrouEne 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
SubrouEnes
• Subs are funcEons, they can have any arguments • The arguments are handed down using the special array @_
• A value can be returned using return Perl: sub multiply {
my (@ops) = @_; return $ops[0] * $ops[1];
}
multiply(2, 3); output: 6
Perl funcEons and parentheses
• Perl funcEons can be wri`en with or without parentheses in most cases, so the statements: index string, search_string;
and
index(string, search_string);
are idenEcal. It is up to you whether you use parentheses.
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, subrouEne 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
Perl Regular expressions (1)
• Very mature, can be highly complex • Can be used for matching (tesEng) or transliteraEon.
• The matching operator `=~ m/… /' is used for tesEng
Perl: if ("This is perl!" =~ m/is/) {
print "Match!";
} output: Match!
Perl Regular expressions (2)
• `^' and `$' match the beginning and end of a string respecEvely.
• You can use character classes (`[' . . . `]'). • You can quanEfy matches using `*', `+' or `?' a�er a character or character class.
• You can quanEfy matches using `{ from , to }’. • `.' matches any character. • Use a backslash (`\') to escape characters like:
`.', `^', `$', `/', `\', `{', `}', `[', `]', . . . .
Perl Regular expressions (3)
Metacharacters in regular expressions: \d Matches a single digit character \w Matches a single word character (a le`er, digit or underscore) \s Matches a whitespace character
. . . . . .
Flags that can follow a regular expression: i Match case insensiEvely g Remember the current posiEon for the next matching
Perl Regular expressions (4)
• Matching subexpressions: – You can match subexpressions by encapsulaEng them with `(' and `)’
– Each subexpression is stored in a variable $1 . . . $n, which can be used later on.
Perl: "This is it!" =~ m/\s(..)/;
print $1;
• QuesEon: What is printed by the above lines?
Perl Regular expressions (4) Search and replace: • Using s/ regexp / replacement /
– Perl: $a = "This is it, or is it?" $a =~ s/\s.(s)/ wa$1/; print $a;
– output: This was it, or is it? • Using the g flag the replacement will be made for all
matches, instead of just the first one. – Perl: $a = "This is it, or is it?” $a =~ s/\s.(s)/ wa$1/g; print $a;
– output: This was it, or was it?
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, . . . 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
File Handling • Opening and closing files • “open” instrucEon takes two arguments: the name of a filehandle and
the name of the file itself (three arguments open). – The filehandle is like a variable which represents the handling of the
file within the script. open (CLIENTS, "clients.txt");
• By default, the file is open in read-‐mode. To open a file in write-‐mode or append-‐mode simply add a “>” or “>>” in front of the file name: open (CLIENTS, ">clients.txt"); or open(my $�, "<", "input.txt") open (CLIENTS, ">>clients.txt");
File Handling: open • “open” instrucEon returns
– true if it managed to open the file, – false otherwise.
• use this value in order to test the success of the operaEon • The following code opens the file in write-‐mode and prints
“Insufficient privileges” if the script doesn't manage to do so. open (CLIENTS, ">clients.txt") or print "Insufficient
privileges\n";
• Remember to always close the files once you're finished with them. If you don't your changes might be lost
close (CLIENTS);
File Handling: write
• Wri5ng into files • Once the file is open in write mode you can write into it
simply by wriEng into its filehandle. • The “print” instrucEon writes things on the screen by
default, but you can specify a filehandle to write into.
open (CLIENTS, ">>clients.txt") or die "Insufficient privileges: $! \n";
print CLIENTS "Mr John Doen"; close (CLIENTS);
File Handling: read • Copying the content of the file into an array
– Each line will then correspond to an element of the array: open(CLIENTS, "clients.txt"); @lines = <CLIENTS>; close(CLIENTS); print @lines;
• Looping through the filehandle – you can loop through the filehandle in a while loop by wriEng while ($line = )
• (think of it as “while” there are lines in the clients file, I'm assigning the current line to $line):
• open (CLIENTS, "clients.txt"); while ($line = <CLIENTS> ) { print $line; } close (CLIENTS);
Valid tests
• These tests are all UNIX tests available to Perl: -‐B true if a binary file -‐d true if directory -‐e true if file exists -‐f true if regular file -‐M returns age in days since last modificaEon -‐r true if readable -‐s returns size of file in bytes -‐T true if text file -‐w true if writable -‐z true if file exists but is empty
Your 3rd Perl script, list of employees ! #open the employeesfile open (EMPLOYEES,"employees.txt"); $line = EMPLOYEE; #for each line while (<$line >) { #remove the carriage return chomp $line; #split the line between tabs #and get the different elements ($name, $department, $salary) = split /\t/, $line; #go to the next line unless the name starts with "Mr “ next unless $name =~ /^Mr /; #go to the next line unless the salary is more than 25000. next unless $salary > 25000; #go to the next line unless the department is R&D. next unless $department eq "R&D"; #since all employees here are male, #remove the parEcle in front of their name $name =~ s/Mr//; print"$name”; } close (EMPLOYEES);
What this script does to the list of employees ?
What can you learn from the example (new things we did not discuss)
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, . . . 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
Packages (1)
• Packages are a way to split up perl code into several pieces
• A package can be included into a piece of code by using use
• Each package represents a new namespace • Create a package
– by creaEng a new file, generally ending in .pm – The code for the class you are creaEng goes in there. – The package name is the same as the filename, this is easier to use, but not obligatory.
Packages (2) • Create a new file, for instance MyPackage.pm • The first line should provide the name of the class in the
package: package MyPackage;
Next comes the code for the package . . . • The file should end with 1; to indicate to the loading
module that the enEre package was read successfully. • Example:
package: package MyPackage; print "WOW, it worked!\n"; 1;
Packages (3)
• To use our package run the following perl code:
#!/usr/bin/perl use MyPackage;
• The output of the program: WOW, it worked!;
Packages (4) • You can refer to variables and filehandles in other packages by
prefixing the iden5fier with the package name and a double colon: $MyPackage::Variable
• You can create subs in a package in the normal way
• They can be called from the program using the package: $MyPackage::the_sub("well?")
• Special code blocks can be used for iniEalizaEon and destrucEon: BEGIN {}, END {}, INIT {}, CHECK {}
• Use perldoc perlmod to get more informaEon
ESA: Perl
• Today: 1. Perl introducEon 2. Basic Perl: types, variables, statements, . . . 3. Regular expression 4. File and I/O 5. Packages 6. Object Oriented Perl 7. DocumentaEon
Objects (1)
“An Object is simply a reference that happens to know which class it belongs to.”
• No special syntax for constructors, you have to make your own
• An object is created using the bless() – The bless() funcEon creates a reference to an object Anything can become blessed
Objects (2) • using a known reference:
package Critter; sub newSub { my $self = {}; bless $self; return $self; }
Note: • {} allocates an anonymous, empty hash and returns a
reference to it • bless takes that reference and tells the object it references
that it's now a Cri`er, and returns the reference h`p://perldoc.perl.org/perlobj.html
Classes (1) “A class is simply a package that happens to provide methods to deal
with object references.” • A class provides the methods for dealing with its reference
• To create an instance of a class (an object) we need an object
constructor. – Most programmers choose to name this object constructor method new, but
in Perl one can use any name
package Critter; sub new { my $self = {}; bless $self; #anonymous {} hash is blessed into the current package return $self; }
h`p://perldoc.perl.org/perlobj.html
Classes (2) • A method is simply a subrou5ne that expects
– an object reference (or a package name, for class methods) as the first argument.
• A constructor can call methods using the reference package Critter; sub newSub { my $self = {}; bless $self; $self->initialize(); return $self; }
h`p://perldoc.perl.org/perlobj.html
Classes (3)
• Classes can be inherited • For that use the two-‐argument version of bless • Bless an object into a class
package Critter; Sub newSub { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(); return $self; }
h`p://perldoc.perl.org/perlobj.html
documentaEon
• BOOKS!, they are in the back of the classroom. . . • Use the web, there are a lot of websites on perl • Check www.perl.com for help. • Find out about perldoc
Material for these slides was taken from h`p://www.perl.com/pub/a/2000/10/begperl1.html
Assignment Create a simple program that can keep track of your mp3's.
1. first program the following using only packages, no OO 1.1. store the names and locaEons of your mp3's in a large array or hash, and allow for addiEonal parameters to be stored as well, like preference (1-‐5 stars), number of Emes played, etc. 1.2. create a simple interface for searching/adding/deleEng/ediEng your database. Make sure to use subs for these funcEons. Make use of RE... 2. (OTIONAL) recreate, using the previous code, the assignment using OO. 2.1 create a main program that can run mulEple databases at the same. Each database must be a separate object instance. Provide an interface that allows the user to choose and switch between the databases at will. 3. (BONUS) add funcEons for (re)storing your databases
Perl Script Exercise: Netswitch • In this exercise, we want to be able to switch between
networks. We defined network configuraEon files in a directory called "networks".
• For instance, here is the content of ./networks/home: interface=eth2 type=dhcp proxy=none And here is the content of ./networks/work: interface=eth1 type=dhcp proxy=www-‐proxy.work.com proxy_port=8080
Assignment
• Further Reading (Watch and summarize the perl 6 talk) – Perl 6, Larry Wall Speaks at Google h`p://www.youtube.com/watch?v=JzIWdJVP-‐wo
– Try to create a table where do you list the main differences between Perl 5 and Perl 6 (google a bit for more differences)