Introduction to Perl

mewstennisSoftware and s/w Development

Nov 4, 2013 (4 years and 2 days ago)

93 views

Scripting Languages Perl Basics Advanced
Introduction to Perl
Grégory Mounié
2012-10-11 jeu.
1/30
Scripting Languages Perl Basics Advanced
Outline
Scripting Languages
Perl
Basics
Advanced
2/30
Scripting Languages Perl Basics Advanced
Why use scripting languages?
Why use Java over C
• Memory management

Portability
• Huge standard library
Why use Perl over Java

No compilation,just interpretation:easy deployment
• No static typing

Expressiveness with high level operators
3/30
Scripting Languages Perl Basics Advanced
What is expressiveness
Expressiveness
Expressiveness is target dependent and user expertise dependent.
The goal is to build complex program with a small number of line
of code.
Common use cases of scripting language:text manipulation,
filtering,prototyping,complex program,graphical/command-line
interface.
4/30
Scripting Languages Perl Basics Advanced
Sh/Bash
process management oriented
• easy to start and control processes and interconnect their I/O

cryptic syntax for everything else
• All variable name start with a $ (idem in Perl)
5/30
Scripting Languages Perl Basics Advanced
Python
Python was designed to be easier to learn than Perl.
• Remove the useless characters
• use indentation as block (be careful with copy-paste!use
automatic indentation)
• object oriented from the start:all functions reference are
stored in a programmable dictionary (overloading at runtime)
• Few strange operators
• Easy to understand.If you can read Java code,you will
understand Python code.
• drawback:increase verbosity
6/30
Scripting Languages Perl Basics Advanced
Ruby
• More expressive than Python
• regexp/string operators as in perl
• a block has arguments (awesome and useful!)
[1,3,5,7,9].find {|v| v*v > 30 }

More readable than Perl

Writing code using explicit Javadoc-like convention

no strange character ($,%,) in the code anymore (save for
blocks)
7/30
Scripting Languages Perl Basics Advanced
History
Perl was written by Larry Wall in 1987.It was designed for text
manipulation,borrowing syntax from C,awk,sed,grep,cut,expr,
and sh.
Mantra of Tim Toady (TIMTOWTDI)
There Is More Than One Way To Do It
8/30
Scripting Languages Perl Basics Advanced
Documentation
Perldoc
Perl has an extensive an high quality documentation.You should
learn to use it.
perldoc perltoc
perldoc perlintro
perldoc perlop
perldoc perlfunc
9/30
Scripting Languages Perl Basics Advanced
Books
There exists numerous books on Perl and web tutorial.
Modern Perl
CC-BY-NC edited book highlighting that “Perl is a language”
http://www.onyxneon.com/books/modern_perl/index.html
Programming Perl
By Larry Wall et Al.Publisher:O’Reilly.
The best SECOND book to read on perl:-)
10/30
Scripting Languages Perl Basics Advanced
Perl is a contextual language
Theorem (Plural)
void,scalar,list contexts
fibonacci(10);#void context,valid to not compute
my $single_value = fibonacci(10);#scalar context,the value
my @all_values = fibonacci(10);#array context,all the fibo values from 1 to 10
11/30
Scripting Languages Perl Basics Advanced
Perl is a contextual language II
Theorem (Gender)
boolean,numeric,string contexts
my $toto ="1"+"1";#numeric context:2
my $titi = 1.1;#string context:"11"
my $tata =!1;#boolean context:false
"titi"=="tata";#numerical equal:true
"titi"eq"tata";#string equal:false
12/30
Scripting Languages Perl Basics Advanced
Perl is a contextual language III
Perl operator and library extensively use a global implicit variable
Theorem (It)
use Modern::Perl;
while (<LINE>) {#read line a line
chomp;#remove"\n"from it
say scalar reverse;
#print the reversed integer
}
“It” may kill you if you sleep [cf Stephen King]
Use local variable ( when using $_;
while(my $_ = <STDIN>) {...
while(my $line = <STDIN>) {...
13/30
Scripting Languages Perl Basics Advanced
Perl is a contextual language IV
Theorem (Implicit array)
@_;
@ARGV;#outside function
Theorem (Not implemented)
...;#the dot operator
trigger an exception “Not implemented” if executed.
14/30
Scripting Languages Perl Basics Advanced
Scalars
The standard variable
Scalar
$toto= 1;
$tata= 1.5;
$titi="2";
$tutu=\@toto;#ref to toto array
15/30
Scripting Languages Perl Basics Advanced
Quoted String
Several string quotations with subtle differences
my $chaine="ls";
print ’titi $chaine’ q{toto $chaine};#titi $chaine
print"titi $chaine"qq/toto $chaine/;#titi ls
print ‘$chaine‘ qx!$chaine!;#list directory
print qw(a b c);#array of 3 string,useful with"use"
print <<EOF;#verbatim inside,’EOF’ for single quote semantic
blab bla
bla bla
EOF
print <<‘COMMAND‘;#string build with a multiline command
date
uname -a
COMMAND
16/30
Scripting Languages Perl Basics Advanced
Loop and test
Exercice:1 to 10
• print values from 0 to 9,on the same line
• the C way
• with the operator
• with local variable and with implicit variable
17/30
Scripting Languages Perl Basics Advanced
Arrays
my @array = ("toto","titi",1,"a");
print $array[0];#print toto
print $#array;#index of last,print 3
print $array[0..1];#array slice
my @sorted = sort @array;
my @rev = reverse @array;
map { print } @array;#apply the block to all element
18/30
Scripting Languages Perl Basics Advanced
Operators change the context
+ - */==!= > < <=>;#numeric operators
eq ne le ge gt lt cmp ~~;#are string operator
Exercice:Add values

Add the first two arguments of your script
and print the result value
• Add all the arguments of your script
and print the result value
• Concatenate all the arguments
and print the result string
19/30
Scripting Languages Perl Basics Advanced
Array and string relation
Split
my @tab = split(";","a;b;c;d;e;f");
Join
my $chaine = join(";",@tab);
20/30
Scripting Languages Perl Basics Advanced
Hashes
Creation
my %mark= ("Eve",17,"Bob",12,"Alice",16);
my %mark= ( Eve => 17,Bob => 12,Alice => 16 );
my $mark_ref = { Eve => 17,Bob => 12,Alice => 16;}
print $mark{"Eve"}."\n";
$mark{Bob}= 2;
$mark_ref->{Bob}=3;
my @students = keys %mark;
my @allmarks = values %mark;
21/30
Scripting Languages Perl Basics Advanced
Regular Expressions
Theorem (Matching,Substitute,Transliteration)
m/regular expression/;#matching
s/regexp/regexp/modifiers;#substitution
tr/regexp/regexp/;#transliteration
Perl allows to test and modify string.
They are often use with operators to select the string.
my $sentence="Hi Guys!\n";
if ($sentence =~ m/Guys/) {
$sentence =~ s/Guys/Girls/;
}
print $sentence;
22/30
Scripting Languages Perl Basics Advanced
Regular Expression
Exercice

starting with the string “hi guys!”

parse a string to change all h by H if it contains Hi
• if it does not contain the word little,insert it between Hi and
Guys
• print the result
23/30
Scripting Languages Perl Basics Advanced
Input/Output
File handle allow integrated manipulation
open(MYFILE,"toto.txt","<");
while(my $line= <MYFILE>) {
print $line;
break;#just read one line
}
my @all_lines= <MYFILE>
close MYFILE;
24/30
Scripting Languages Perl Basics Advanced
Subroutine
You may define subroutine (function) Almost no parameter
management
sub fibo {
my $n = shift;
return 1 if $n < 2;
return fibo($n - 1) + fibo($n - 2);
}
print fibo(10);
25/30
Scripting Languages Perl Basics Advanced
Exercice:user
Human user of the computer
• File/etc/passwd

A human needs a/home
• User using firefox
• File/Pipe
• Process list with “ps aux”
• split/splice for column parsing
• One user,One name (hash)
• Get the first three images of xkcd
• to get the text of the URL of the 5th image
• File/Pipe

rename file to include the number
26/30
Scripting Languages Perl Basics Advanced
References
No pointer,just references.
27/30
Scripting Languages Perl Basics Advanced
Complex Data Structures
28/30
Scripting Languages Perl Basics Advanced
Package
29/30
Scripting Languages Perl Basics Advanced
Object Perl

ICS examples
30/30