Introduction To Perl
-
Upload
andres-maneiro -
Category
Business
-
view
1.183 -
download
3
Transcript of Introduction To Perl
Master on Free Software
What is Perl?
● Interpreted programming language.● Created by Larry Wall in 1987.● Optimized for processing text files.● Widely used in Unix system administration.● Designed to be practical rather than elegant.● Combines features from sh, awk and sed.● Huge amount of modules available at cpan.org● Influenced popular languages such as Python and PHP.
● Popularity in decline, Perl 6 delayed.
Master on Free Software
Basic features
● Modular● Basic object-oriented programming support.● Unicode support.● Internationalization support.● Powerful regular expression operators.● Built-in debugger.
Master on Free Software
Syntax
● Similar to C● Files start with '#!/usr/bin/perl -w'● Statements end with ' ; '● Comments with '#' (similar to sh)● Variable declaration is not necessary (but it can be enforced)
● Same basic operators as in C: +, -, =, +=, -=, ++, !=, ==, ... (see perlop(1) manpage)
Master on Free Software
Data types: scalar (1)
● Contains a single value● Can be a number or a string● Automatic conversion● Can also contain the undef value, similar to NULL in C.
● A scalar variable is named with a '$' sign● Example:$a = 10;$b = $a + 10;$c = "The result is $b";print "$c\n";
Master on Free Software
Data types: scalar(2)
● A scalar also has a boolean value.● Scalars with values other than 0, “0” (string), “” (empty string) or undef are TRUE.
● Logical operators: ||, &&, !, or, and, not● Binary operators: &, |, ~, ^, <<, >>● Conditional operator: ($a == $b) ? 1 : 2● Comparator: $a <=> $b, which returns
● -1 if $a < $b● 0 if $a == $b● 1 if $a > $b
Master on Free Software
Data types: scalar (3)
● Assignment:$a = $b;$a = 15.32;$a = “E = mc2”;
● Numeric operations:$a += $b + 10;$a = $b ** 3;
● Numeric comparisons:$a == $b($a + $b) != ($c + 10*$d)
Master on Free Software
Data types: scalar (4)
● Possible scalar numeric expressions:12345123.45.23E-104_294_967_296 # To improve legibility0xff # Hexadecimal0377 # Octal0b011011 # Binary
Master on Free Software
Data types: scalar (5)
● Difference between “” and ''. The former expands all variables in the string:$a = 100;$b = "Num $a"; # Num 100$c = 'Num $a'; # Num $a$d = "Num \$a"; # Num $a
● Use { } to avoid ambiguities:$d = 100;print "Distance: ${d}m\n";
Master on Free Software
Data types: scalar (6)
● String concatenation:$a = $b . “$c 10” . “\n”;
● String comparison:$a eq $b$a lt “Some text”
● Equivalences:Numbers: + < > <= >= == != <=>Strings: . lt gt le ge eq ne cmp
Master on Free Software
Data types: scalar (and 7)
● An non-initialized scalar is always undef:● To check whether a scalar is defined or not:
if (!defined($a)) { $a = 0;}
● Any variable can be undefined at any time:$a = 5;undef $a; # also, $a = undef;
Master on Free Software
Exercise 1
● Given the following piece of code:
#!/usr/bin/perl -w
$a = 10;$b = 20;
● Complete the program so that it multiplies $a by $b to produce the following output:
$a multiplied by $b is 200
Master on Free Software
Data types: array (1)
● An array is a variable that holds a list of scalars.● A list is expressed this way:
( val1, val2, val3, ..., valn )● Array variables are named with a '@' sign:
@l = ( “Text $a”, 15, $c );● Scalar and array variables are in different name spaces. These are different variables and can coexist:$var = 10;@var = ( 12, “Text” );
Master on Free Software
Data types: array (2)
● To access data in an array the [ ] operator is used.
● The first element has index 0● list[0] is an scalar, so it must use '$' and not '@':
@list = ( 10, 20, 30, 40 );$a = $list[0]; # Right$b = @list[0]; # Wrong !
● Thus, '$var[0]' is an element of '@var' and has nothing to do with '$var'.
Master on Free Software
Data types: array (3)
● Assignment between arrays is very flexible:@list = (54, 'ab', 23, 15.3, 43);@list2 = @list;@list3 = @list[0,2..4];@list4 = ($list[0], $list[3]);@list5 = (0, 1, 2, 3, 4, 5);@list5[1..3] = @list[0..2];@list6 = (2..6); # (2, 3, 4, 5, 6)($a, $b) = ($c, $d);($a, $b, $c, $d) = @list[0..3];($a, undef, $b, $c, $d) = @list;($a, $b) = (1, 4, 5, 6, 7)[3,4];
Master on Free Software
Data types: array (4)
● Arrays are automatically “flattened”.@a = ( 1, 2 );@b = ( @a, 3, @a, ( 6, 7 ) );($a, $b, @rest) = ( 1, 2, 3, 4, 5 );@d = ( 1, 2, (), 3, 4 ); # 4 elems.
● “$#var” is the last index of “@var”. So “$#var + 1” is the number of elements@list = ( “bla”, 15.2, 23 );$a = $#list; # now $a is 2
● “$#var” can be modified to alter the length of the array:$#list = 0; # Leave just the first element$#list = -1; # Empty the list
Master on Free Software
Data types: array (5)
● Functions 'push' and 'pop' are used to add and remove elements from the end of an array.@a = ( 1, 2 ); # @a is (1, 2)push (@a, 3, 4); # @a is (1, 2, 3, 4)$b = pop(@a); # $b is 4, @a is (1, 2, 3)
● Functions 'shift' and 'unshift' are similar, but they modify the beginning of the array@a = (3, 4); # @a is (3, 4)unshift (@a, 1, 2); # @a is (1, 2, 3, 4)$b = shift (@a); # $b is 1, @a is (2, 3, 4)
Master on Free Software
Data types: array (and 6)
● 'sort' sorts an array (the original one is not modified).@a = (4, 3, 2, 1);@b = sort(@a);
● 'reverse' is used to reverse an array:@a = (4, 3, 2, 1);@b = reverse(@a);
Master on Free Software
Exercise 2
● Ask the user to type some numbers, and order them.
● How to read input from the user:$a = <STDIN>; chomp($a);
● It should be something like this:$ ./exercise-1.plType number 1/4: 20Type number 2/4: 15Type number 3/4: 8Type number 4/4: 18Thanks, your numbers are: 8 15 18 20
Master on Free Software
Data types: hash (1)
● A hash is an associative array (key -> value)● Keys are strings.● Values are scalars.● Hashes are named with '%'
%person = ( 'name', 'Pepe', 'age', 25);
● This syntax is equivalente, but more readable%person = ( name => 'Pepe', age => 25);
Master on Free Software
Data types: hash (2)
● To access elements the { } operator is used.● New entries can be created at any time.
$a = $person{name};$person{surname} = “Barreiro”;$b = 'name';print $person{$b};
● You can access many elements in the same expression:@a = @person{name,surname};
Master on Free Software
Data types: hash (and 3)
● 'keys' returns the keys of a hash%person = (name => 'Pepe', age => 32);@l = keys(%person);
● 'values' is a similar function, but returns values instead of keys.
Master on Free Software
References (1)
● Similar to pointers in other languages.● Used to hold complex data structures inside scalars.
● They are stored in scalar variables.$scalarref = \$scalarvar;$arrayref = \@arrayvar;$hashrref = \%hashvar;
Master on Free Software
References (2)
● Using references:$scalarvar = $$scalarref;$scalarvar = ${$scalarref}; # More readable@l = @{$arrayref};$$arrayref[0] = 1;$$hashref{key} = 'value';
● You can also create anonymous refs to arrays and hashes using [ ] and { }:$arrayref = [ 1, 2, 3 ];$arrayref2 = [ 1, 2, ['a', 'b'], 3 ];$hashref = { name => 'Pepe', age => 25 };
Master on Free Software
References (and 3)
● Complex data structures using anonymous refs%hash = ( name => 'Pepe', age => '25', children => ['Ana', 'Roi']);@children = @{$hash{children}};print "$hash{name} is ". "$hash{age} years old and has ". ($#children+1)." children\n";
Master on Free Software
Exercise 3
● Given the following code:
#!/usr/bin/perl -w
$a = [ 10, 45, 13, 53, 62 ];
● Complete the program to count the number of elements in the list to produce this output:
The number of elements in $a is 5
Master on Free Software
Control structures: if
● Similar to C Braces { } are always required:if ( condition ) { ...} else { ...}
if ( condition ) { ...} elsif ( condition) { ...}
Master on Free Software
Control structures: while
● Similar to C Braces { } are always required:while ( condition ) { ...}
do { ...} while (condition);
Master on Free Software
Control structures:for and foreach
● 'for' is similar to C:for ($i = 0; $i < 10; $i++) { print "\$i contains $i\n";}
● 'foreach' iterates over the elements of an array:foreach $val (@array) { print "$val\n";}
Master on Free Software
Exercise 4
● Given the following code:@a = ( { name => "Manolo", age => 25 }, { name => "Sara", age => 34 });
● Write a program to produce this output by reading @a:Manolo is 25 years oldSara is 34 years old
● It should still work no matter the number of elements in @a.
Master on Free Software
Files (1)
● To open and close files, we use “open” and “close”open (MYFILE, “/etc/passwd”);close (MYFILE);
● “FILE” is a file descriptor. It can be any word. Some predefined descriptors are “STDIN”, “STDOUT” and “STDERR”.
Master on Free Software
Files (2)
● There are many ways to open a file:open(FILE1,'/tmp/somefile'); # Read-onlyopen(FILE2,'</tmp/somefile'); # Read-onlyopen(FILE3,'+</tmp/somefile'); # Read-writeopen(FILE4,'>/tmp/somefile'); # Truncateopen(FILE5,'>>/tmp/somefile'); # Append
Master on Free Software
Files (and 3)
● To write to a file we use “print”print MYFILE “Some text\n”;
● To read from a file we use “<>”$a = <MYFILE>; # Read a single line@b = <MYFILE>; # Read the rest of the file
● If we omit the file descriptor, STDIN and STDOUT are used:print “Hello world\n”; # Writes to STDOUT$a = <>; # Reads from STDIN
Master on Free Software
Special variables
● $_ is an implicit variable for several contexts (input/output, pattern matching, ... used mostly in loops):print $_;print; # Same result
● $0 is the name of the executable. @ARGV is the list of parameters.
● %ENV contains environment variables.● Complete list in perlvar(5) man page.
Master on Free Software
Reading files using loops
● It's very common to read lines from a file using a loop:$i = 1;while ($a = <STDIN>) {
print “$i: $a”;$i++;
}● It's also very common to use $_:
$i = 1;while (<STDIN>) {
print “$i: $_”; $i++;}
Master on Free Software
Other control structures
● 'unless' and 'until' are the opposite of 'if' and 'while'.
● Alternate syntax for conditions with a single statement:if ($a < 0) { print “\$a is negative\n”;}print “\$a is negative\n” if ($a < 0);
● You can also use 'and' and 'or'($a < 0) and print “\$a is negative\n”;($a >= 0) or print “\$a is negative\n”;
Master on Free Software
Exercise 5
● Write a program that copies a file into another, reversing the lines. Print the number of lines at the end.
$ ./exercise-5.pl input.txt output.txtDone! The file had 5 lines!
$ cat out.txtLast lineThe one before the lastThird lineSecond lineFirst line
Master on Free Software
Pattern matching (1)
● The ' =~ ' operator binds a scalar expression to a pattern match. The regexp goes between / /:if ($a =~ /^From/) { print “This line starts with 'From'\n”;}
● perlop(1) and perlre(1) man pages explain all pattern matching types and regular expressions.
Master on Free Software
Pattern matching (2)
● The most basic form of pattern matching is “/regexp/args”($a =~ /^from/i) # 'i' means case-insensitive
● Subexpressions can be defined to obtain parts of the match:if ($a =~ /^From: (.*) <(.*)>/) { print “Name is $1\n”; print “E-mail address is $2\n”;}
Master on Free Software
Pattern matching (and 3)
● Operator “s/regexp/subst/args” works very much like the 'sed' command.$a =~ s/Lugo/Ourense/;$b =~ s/.*<(.*)>.*/$1/;
● Operator “tr/pattern/subst/args” works very much like the 'tr' command.$var =~ tr/A-Z/a-z/;
● Operator “!~” is like “=~” but the boolean result is reversed:if ($a !~ /London/) { print “$a does not contain the word London\n”;}
Master on Free Software
Exercise 6
● Write a program that prints the contents of a file, omitting the lines starting with “Hide” and replacing “this” for “that”
$ cat file.txtI like thisHide meI don't like this
$ ./exercise-6.pl file.txtI like thatI don't like that
Master on Free Software
Some useful functions and operators (1)
● Man page perlfunc(1) contains a list of all standard Perl functions.
● 'chomp' removes the trailing “\n” from a string (if present).
● `` captures the output of an external command$date = `/bin/date`;chomp($date);print “Date is $date according to /bin/date\n”;
Master on Free Software
Some useful functions and operators (and 2)
● 'system' executes an external command:system(“cp /tmp/file1 /tmp/file2”);system(“cp”,”/tmp/file1”,”/tmp/file2”); # Safer
● 'glob' obtains a list of files matching a pattern.@tmpfiles = glob('*.tmp');@tmpfiles = <*.tmp>; # Alternate syntax
● 'split' splits a string, 'join' does the opposite.@parts = split(/:/, 'root:Fddfxf:0');($login, $pass, $uid) = @parts;$a = join(':',$login,$pass,$uid);
Master on Free Software
Exercise 7
● Write a program that reads /etc/passwd to produce this output:
$ ./exercise-7.plThe user ID of root is 0The user ID of daemon is 1The user ID of bin is 2...
● Hint: you have to read these fields:
root:x:0:0:root:/root:/bin/bash
Master on Free Software
Exercise 8
● Write a program that creates a backup of a directory storing the date in the file name:$ ./exercise-8.plBackup done!$ ls /tmp/backupsbackup-31-07-2008.tar.gzbackup-01-08-2008.tar.gz
● Hint: to backup a directory using tar...$ tar -C /some/dir -czf /tmp/backups/out.tar.gz
● Hint: to get the current date...$ date +%d-%m-%Y
Master on Free Software
Subroutines (1)
● A basic subroutine looks like this:sub some_name {
my $localvar1;my $localvar2;# ...return $retvalue;
}● 'my' is used to declare variables. Can be used for global variables too.my $a;my ($b, @c, %d);
Master on Free Software
Subroutines (2)
● A function receives a list of parameters.● '@_' is the variable containing them all.● Subroutines don't usually have prototypes.● The number of parameters is variable (it's a list).
● The usual way of reading parameters is with 'shift':my $a = shift(@_);my $b = shift; # Equivalent, @_ is implicit
Master on Free Software
Subroutines (and 3)
● 'return' is used to return a value.● Returned values can be scalars, arrays or hashes.
● Subroutines can be called recursively.
Master on Free Software
Exercise 9
● Modify the program from exercise 7 so that it uses subroutines.
Master on Free Software
Using modules (1)
● Statement “use Foo;” loads module 'Foo'.● To use variables and subroutines inside each module in some cases it's necessary to use the “::” operator (check module's man page).use POSIX;my $dir;$dir = POSIX::opendir(“/tmp”);
Master on Free Software
Using modules (and 2)
● Most modules are object-oriented.● To use objects there's the “->” operator:
use Net::Ping;my $p = Net::Ping->new();$p->ping('192.168.1.1');
● Special module named 'strict' makes the Perl interpreter stricter and safer. It also enforces declaration of variables:use strict;
Master on Free Software
Sample program (1)
● This program connects to a POP3 server and shows the Subject of every message.
● We use the 'Net::POP3' module.● Configuration file is under '/etc/pop3.cfg', with the following syntax:host=pop3.somehost.comuser=someuserpass=somepasswd
Master on Free Software
Sample program (2)
● First: load modules and declare variables.
#!/usr/bin/perl -w
use strict;use Net::POP3;
my ($host, $user, $pass);my $cfg = '/etc/pop3.cfg';
Master on Free Software
Sample program (3)
● Read configuration file
open (CFGFILE, $cfg);
while (<CFGFILE>) {$host = $1 if (/host=(.*)/);$user = $1 if (/user=(.*)/);$pass = $1 if (/pass=(.*)/);
}
close (CFGFILE);
Master on Free Software
Sample program (and 4)
● Connect and show data
my $pop = Net::POP3->new($host);my $msgs = $pop->login($user,$pass);
for (my $i = 1; $i <= $msgs; $i++) { my @lines = @{$pop->top($i)}; foreach (@lines) { print "$1\n" if (/^Subject: (.*)/); } }
$pop->close();print "$msgs messages have been processed\n";
Master on Free Software
Exercise 10
● Write a program that displays the top 10 artists from any Last.fm user.
● Last.fm API here (see “User Profile Data”):http://www.audioscrobbler.net/data/webservices/
● Use the LWP::Simple Perl module.$ apt-get install libwww-perl$ man LWP::Simple
Master on Free Software
Exercise 11
● Type a Last.fm user● Get that user's first 5 neighbours● Get top 10 artists from each neighbour● Display a list of all artists. If an artist is repeated, display it only once along with the number of occurrences.
● The list must be sorted. Most popular artists must appear first.