500 likes | 842 Views
Perl. ESA 2011/2012 Adam Belloum a.s.z.belloum@uva.nl. Material Prepared by Eelco Schatborn. ESA: Perl. Today: 1. Perl introduction 2. Basic Perl: types, variables, statements, . . . 3. Object Oriented Perl 4. Documentation. Regular Expressions.
E N D
Perl ESA 2011/2012 Adam Belloum a.s.z.belloum@uva.nl Material Prepared by EelcoSchatborn
ESA: Perl • Today: 1. Perl introduction 2. Basic Perl: types, variables, statements, . . . 3. Object Oriented Perl 4. Documentation
Regular Expressions • “Practical Extraction and Report Language"? • Developed by Larry Wall, as system administrator at NASA, in the late 1980s • Created as a way to make report processing easier. • It's an interpreted language, but can be compiled as well • Is now being used for: • System administration automation • Glue between systems, conversion • CGI backend for websites • Much more. . .
Simple Perl A simple program to start with: #!/usr/bin/perl print "Hi there!\n"; print "This is a very"; print "\tsimple program.\n";
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". • In fact, you can literally write a Perl script your way, with your own style. Some people even do "poetry" in Perl • Because of this, some Perl scripts can be verydifficult to read. Writing them though, is always a pleasure. Clement Lefebvre: http://www.linuxforums.org/articles/learn-perl-in-10-easy-lessons-lesson-1_120.html
Another Perl script http://www.perlmonks.org/?node_id=1590
The Perl interpreter • Perl is an interpreted 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: $ perlmyScript.pl Clement Lefebvre: http://www.linuxforums.org/articles/learn-perl-in-10-easy-lessons-lesson-1_120.html
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. Floating point numbers: • “one and a quarter": 1.25 • “7.25 times 10 to the 45th power": 7.25e45. • “negative 12 times 10 to the -24th": -12e-24.
String manipulation • Escape sequences represent special characters: \n, \t, . . . • Only in double quoted (‘ “ ’) strings • For verbatim strings use single quotes (‘ ‘ ’) Perl: print "Hi there! \n \t It's me.\n"; print 'And me \n \tas well!'; • output: Hi there! It's me. And me \n \t as well!
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 punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores .
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 = $resultn"; Make the script executable: $ chmoda+rxcalculator.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
Scalar variables • Start with the dollar (‘$’) sign. • Can be a number or a string (or an object reference) • The usual operators apply: +, -, *, /, =, +=, -=, *=, /=, ++, -- • Perl automatically converts between numbers and strings. $n = 8 + "2" ! $n holds "10” • concatenation of strings using a period `.’ $n = 8 . "2" ! $nholds "82”
Array variables (1) • Start with the at (`@') sign. • Arrays represent ordered lists of scalars. • Definition using parenthesized, comma delimited lists: @numbers = ( 1, 2, 4, 8, 16 ); @strings = ( "one", "two", "3" ); • The index of an array starts at 0, not 1 • Processing single items as a scalar using `[' and `]': print $strings[1]; # prints "two" $strings[1] = "2"; # we changed @strings
Array variables (2) • Arrays can be created using scalar assignment: $names[0] = "Eelco"; creates @names to hold ("Eelco") • The length of an array: $#strings, returns the number of items in the array minus one. If equal to -1, the array does not exist or is empty. print $#strings; ! prints 2 • Assignment to $#strings changes the size of the array. $#strings = 0; @strings now holds ( "one" )
Hash variables (1) • Start with the percentage (`%') sign. • Made up of keys and values. • Each key has exactly one corresponding value. • Definition 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"; ! Changed %numbers
Hash variables (2) • Like arrays, hashes can be created using scalar assignment: $strings{"1"} = "one"; creates %strings to hold ( "1" => "one" ) • The keys <hash> function will return an array filled with all the keys in the hash. print keys%numbers; ! prints ( "one", "two" )
Flow control • A block of code contain a list of statements. • Code blocks are delimited by `{' and `}’ • A perl program is also a block of code. • Flow control can be excreted using looping or conditional Statements.
Looping Statements • For loops are for repetition • 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 ) { . . . }
Conditional Statements (1) • Conditional statements: if ( test ) { . . . } elsif{ . . . } else { . . . } unless ( test ) { . . . } else { . . . } while ( test ) { . . . } until ( test ) { . . . } • The usual comparison operators for testing: <, >, ==, !=, <=, >= • But comparison of strings using `eq'. 0xa == " 10 " true 0xa eq " 10 " false
Conditional Statements (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"; }
Conditional Statements (3) $a = 0; while ($a != 3) { $a++; print "Counting up to $a...\n"; } until ($a == 0) { $a--; print "Counting down to $a...\n"; }
Play Around 1 • At this point you have some basic knowledge of Perl syntax and a few simple toys to play with. Try writing some simple programs with them. • Here are two suggestions, one simple and the other a little more complex: • A word frequency counter. How often does each word show up in an array of words? Print out a report. (Hint: Use a hash to count of the number of appearances of each word.) • Given a month and the day of the week that's the first of that month, print a calendar for the month.
Idea of a solution • Use split to divide you string into words, separator “ “ . • For $words(split(‘ ‘, $strings) ) • $count{$words} +=1;
Perl Regular expressions (1) • Very mature, can be highly complex • Can be used for matching (testing) or transliteration. • Simple syntax is / . . . / • The matching operator `=~' is used for testing Perl: if ("This is perl!" =~ /is/) { print "Match!"; } output: Match!
Perl Regular expressions (2) • `^' and `$' match the beginning and end of a string respectively. • You can use character classes (`[' . . . `]'). • You can quantify matches using `*', `+' or `?' after a character or character class. • `.' 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 letter, digit or underscore) \s Matches a whitespace character . . . . . . Flags that can follow a regular expression: i Match case insensitively g Remember the current position for the next matching
Perl Regular expressions (4) • Matching subexpressions: • You can match subexpressions by encapsulating them with `(' and `)’ • Each subexpression is stored in a variable $1 . . . $n, which can be used later on. Perl: "This is it!" =~ /\s(..)/; print $1; • Question: 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?
File Handling • The Perl language was designed to make file handling easy and efficient, so you'll probably won't have any problem opening files and reading them. • Opening and closing files • “open” instruction takes two arguments: the name of a filehandleand the name of the file itself. • filehandleis 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"); open (CLIENTS, ">>clients.txt");
File Handling: open • The “open” instruction returns • true if it managed to open the file, false otherwise. • You can use the return value to test the success of the operation 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. • to close a file, simply use the “close” instruction on the filehandle: close (CLIENTS);
File Handling: write • Writing into files • Once the file is open in write mode you can write into it simply by writing into its filehandle. • The “print” instruction writes things on the screen by default, but you can specify a filehandle to write into. open (CLIENTS, ">>clients.txt") or die "Insufficientprivilegesn";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. Here is an example: 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 writing while($line = ) • think of it as “while” which assign lines in the clients file to $line open (CLIENTS, "clients.txt");while ($line = <CLIENTS>) {print $line;}close (CLIENTS);
Your third Perl script, list of employees ! #open the employeesfile open (EMPLOYEES,"employees.txt"); #for each line while (<EMPLOYEES>) { #remove the carriage return $line=$_; 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 particle 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)
Play Around 2 • A rule of good writing is "avoid the passive voice." Instead of The report was read by Carl, say Carl read the report. • Write a program that reads a file of sentences (one per line), detects and eliminates the passive voice, and prints the result. (Don't worry about irregular verbs or capitalization, though.)
Subroutines • Subs are functions, 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
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 creating a new file, generally ending in .pm • The code for the class you are creating goes in there. • The classname often 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 entire package was read successfully. • Example: packageMyPackage; print "WOW, it worked!\n"; 1;
Packages (3) • To use our package run the following perl code: #!/usr/bin/perl useMyPackage; • The output of the program: WOW, it worked!;
Packages (4) • You can refer to variables and filehandlesin other packages by prefixing the identifier with the package name and a double colon: $Package::Variable • You can create subs in a package in the normal way • They can be called from the program using the package: $Package::the_sub("well?") • Special code blocks can be used for initialization and destruction: INIT {}, CHECK {}, BEGIN {}, END {} • Use perldocperlmod to get more information
Objects (1) “An Object is simply areferencethat happens to know which class it belongs to.” • No special syntax for constructors, you have to make your own • A constructor is merely a subroutine that returns a reference to something "blessed" into a class, generally the class that the subroutine is defined in. “ • An object is created using the bless() • The bless() function creates a reference to an object http://perldoc.perl.org/perlobj.html#An-Object-is-Simply-a-Reference
Objects (2) • Using an anonymous reference (Here is a typical constructor) package Critter; sub new { bless {} } 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 Critter, and returns the reference • Using anonymous objects the class will not know itself • using a known reference: package Critter; sub new { my $self = {}; bless $self; return $self; } http://perldoc.perl.org/perlobj.html#An-Object-is-Simply-a-Reference
Classes (1) “A class is simply a packagethat happens to provide methods to deal with object references.” • A class provides the methods for dealing with its reference • Construction by defining a constructor function, generally new() package Critter; sub new { my $self = {}; bless $self; return $self; } http://perldoc.perl.org/perlobj.html
Classes (2) • A method is simply a subroutine that expects as the first argument an: • object reference • a package name (for a class method) • A constructor can call methods using the reference sub new { my $self = {}; bless $self; $self->initialize(); return $self; } http://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 Sub new { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(); return $self; } http://perldoc.perl.org/perlobj.html
documentation • 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 http://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 locations of your mp3's in a large array or hash, and allow for additional parameters to be stored as well, like preference (1-5 stars), number of times played, etc. 1.2. create a simple interface for searching/adding/deleting/editing your database. Make sure to use subs for these functions. Make use of RE... 2. recreate, using the previous code, the assignment using OO [OPTIONAL]. 2.1 create a main program that can run multiple 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 functions for (re)storing your databases
Perl Script Exercise: Netswitch • In this exercise, we want to be able to switch between networks. We defined network configuration 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
Perl Script Exercise: Netswitch • The Perl script should: • takes a network name as its command line argument, • opens the file with the same name from ./networks and sets the network interface with the data taken from the content of that file