SlideShare a Scribd company logo
1 of 141
Download to read offline
Corso
introduttivo a
Perl
Pisa, 22 Ottobre 2009
Corso
introduttivo a
Perl
Stefano Rodighiero
http://www.stefanorodighiero.net
stefano.rodighiero@gmail.com
Twitter: @larsen
Cosa potete
aspettarvi da
questo corso
Sapere scrivere dei semplici programmi Perl
Sapere leggere programmi Perl
Sapere dove trovare altra documentazione
Corso
introduttivo a
Perl
•Variabili
•Riferimenti e strutture dati
•Subroutine
•I/O
•Espressioni regolari
•Moduli
•CPAN
Perl
~1987, Larry Wall
Perl 4.0
1991, Camel Book
Perl 5
1994, Perl come lo conosciamo ora
Perl 5.10
Perl 5.11
Perl 5.11
5.11.1 l'altro ieri!
Perl 6
???
Rakudo
Rakudo *
~ Primavera 2010
Perl
PERL
Practical Extraction and Report Language
Pathologically Eclectic Rubbish Lister
Polymorphic Existential Recursive Lambdas
perl
Perl
TIMTOWTDI
Rendere facili le cose facili, possibili le cose difficili
Simile ad un linguaggio naturale
Procurarsi
Perl
Se usate un sistema *NIX, molto
probabilmente lo avete già
Per Windows, ActiveState Perl oppure
Strawberry Perl
Come cercare
aiuto
perldoc
mongers@perl.it
#perl.it
www.perlmonks.org
Variabili ::
$
Scalari
Un valore singolo
Numeri e stringhe di caratteri
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
@
Array
Un insieme ordinato di valori scalari
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
%
Hash
Un insieme (non ordinato)
di coppie chiave-valore scalare
Fondamentale!
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
Riferimenti e
strutture dati ::
Riferimenti e
strutture dati ::
Una questione in sospeso...
Riferimenti e
strutture dati ::
Una questione in sospeso...
use feature 'say';

# E` esattamente equivalente a questo

my @array = (1, 2, 3, 4, 5, 6);
say $array[1];

# Come si fa allora un array di array?
# Lo vediamo piu` tardi...
Uno scalare
può contenere:
Un numero.
Una stringa.
Un riferimento
ad un'altra
variabile.
my @array = (1, 2, 3, 4, 5, 6);
my @array = ((1, 2, 3), (4, 5, 6));
my @array = ((1, 2, 3), (4, 5, 6));


                 @array
                   1
                   2
                   3
                   4
                   5
                   6
my @array = ((1, 2, 3), (4, 5, 6));


            @array
                        1
              •         2
                        3
              4
              5
              6
my @array = ((1, 2, 3), (4, 5, 6));


            @array
                        1
              •         2
                        3


              •         4
                        5
                        6
my @array = ([1, 2, 3], [4, 5, 6]);


            @array
                        1
              •         2
                        3


              •         4
                        5
                        6
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
Subroutine ::
Subroutine ::
Codice riusabile (Don't repeat yourself: DRY)
Strumento di astrazione
Configurabilità del comportamento
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
Controllo
del flusso ::
Esecuzione
condizionale ::
if ... elsif ... else
if (EXPR) BLOCK
ne,
if (EXPR) BLOCK                ssio
                       e spre          tata
                  Un'             valu
                        e v iene
                   ch            sto
                             nte
                    i n co
                          ole ano.
                      bo
ne,
                                                ssio
Falso                             Un'   e spre
                                         e v iene
                                                   valu
                                                        tata
                                    ch            sto
0 (il numero zero)                            nte
                                     i n co
                                               ano.
'' (la stringa vuota)
'0' (una stringa che contiene il singolo ole
                                       bo
carattere corrispondente alla cifra zero)
undef
ne,
                              ssio
Vero             Un'  e spre
                       e v iene
                                 valu
                                      tata
                  ch            sto
Tutto il resto              nte
                   i n co
                         ole ano.
                     bo
ne,
if (EXPR) BLOCK                           ssio
                                  e spre          tata
                             Un'             valu
                                   e v iene
                              ch            sto
                                        nte
                               i n co
                                     ole ano.
                                 bo
   Un in
         sieme
  istru         di
        zioni,
 da gr         delim
       affe.         itate
if (EXPR) BLOCK
if (EXPR) BLOCK
else BLOCK
if (EXPR) BLOCK
elsif (EXPR) BLOCK
else BLOCK
if (EXPR) BLOCK
elsif (EXPR) BLOCK
else BLOCK
given ... when
Come switch e
case in altri
linguaggi
given (EXPR) {
  when (EXPR) BLOCK
  ...
  default BLOCK
}
given (EXPR) {
  when (EXPR) BLOCK
  ...
  default BLOCK
}
given (EXPR) {
  when (EXPR) BLOCK
  ...
  default BLOCK
}
given (EXPR) {
  when (EXPR) BLOCK
  ...
  default BLOCK
}
Modificatori
di statement ::
...   if EXPR;
...   unless EXPR;
...   while EXPR;
...   until EXPR;
...   foreach EXPR;
Iterazione ::
for / foreach
foreach (LIST)
  BLOCK
foreach (LIST)
  BLOCK
lori
                              ei va
                     lis ta d       lare
foreach (LIST)   La           i cic
                          ual
                  s ui q
  BLOCK
lori
                                   ei va
                          lis ta d       lare
foreach (LIST)        La           i cic
                               ual
                       s ui q
  BLOCK
              Un in
                    sieme
             istru         di
                   zioni,
            da gr         delim
                  affe.         itate
lori
                                               ei va
                                      lis ta d       lare
foreach (LIST)                    La           i cic
                                           ual
                                   s ui q
  BLOCK
                           Un in
                                 sieme
                          istru         di
    All'in                      zioni,
           terno         da gr         delim
   blocc          del          affe.         itate
           o, il
  eleme          singo
         nto è         lo
                 $_
foreach my $var (LIST)
  BLOCK
All'interno del
blocco è tipico
usare ulteriori
istruzioni per il
controllo di
flusso
next
redo
last
while
while (EXPR)
  BLOCK
one
                    spr essi
               L 'e          tat a in
while (EXPR)            valu
                viene
  BLOCK                  sto
                  c onte
                           ano
                    b oole
one
                              spr essi
                         L 'e          tat a in
while (EXPR)                      valu
                          viene
  BLOCK                            sto
                            c onte
                                     ano
                              b oole
            Il bl
                  occo
           esegu          viene
                  ito f
          condi         inchè
                zione           la
                        è vera
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
I/O ::
Lettura da file
open my $fh, "<", $filename;
open my $fh, "<", $filename;

 Il
      fil
         eh
              an
                dle
open my $fh, "<", $filename;

 Il                    In
      fil
         eh           ap      ch
              an        rir     em
                dle         ei      od
                               lf      ali
                                 ile       tà
                                     ?
open my $fh, "<", $filename;

 Il




                                                       e
                                                      fil
      fil              In
         eh           ap      ch




                                                     del
              an        rir     em
                dle         ei      od




                                                 me
                               lf      ali
                                 ile       tà



                                                no
                                     ?

                                                Il
Modalità di
apertura
<               Lettura
>              Scrittura
>>              Append
+<            Read/write
     Write/read (se il file esiste lo
+>
               tronca
my $line = <$fh>;
my $line = <>;


                             ata,
                  a ab brevi
           Form       ST  DIN
            leg ge da
my $line = <DATA>;

                         pec iale,
                For ma s
                        dal
                 legge           ATA
                            le D
                  file hand
close $fh;
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
Espressioni
regolari ::
Un linguaggio
dentro il
linguaggio.
Espressioni
regolari
Una maniera per esprimere insiemi di
stringhe di caratteri
Riconoscimento di formati
Manipolazione del testo
/foo/
foo
foo   bar foo baz
foo foo baz
 bar
foo foo baz
bar
bar foo baz
 foo
bar foo baz
    foo
bar foo baz
    foo
 Esercizi
http://www.stefanorodighiero.net/perl101/examples.tgz
Moduli ::
Insieme alle
subroutine,
ulteriore
strumento di
astrazione

Esercizi
Ulteriori cenni
su strict
Quando strict
si lamenta per
le variabili?
1.
La variabile non è
completamente
qualificata.
2.
Non è una
variabile
lessicale
precedentemente
definita.
3.
Non è una
variabile package
precedentemente
definita con vars
CPAN ::
search.cpan.org
$ perl -MCPAN -e shell
$ cpan
Qualche modulo
utile
DBI
CGI
Template
Getopt::Long
DateTime
Regexp::Common
HTML::Parser::*
XML::LibXML
Moose
Bibliografia ::
Bibliografia ::
Programming Perl
Larry Wall et al.
Oreilly, 1995
Bibliografia ::
Pocket Perl
Stefano Rodighiero
Apogeo, 2008

More Related Content

More from Stefano Rodighiero

More from Stefano Rodighiero (7)

On the most excellent theory of time travel, poetic revolutions, and dynamic ...
On the most excellent theory of time travel, poetic revolutions, and dynamic ...On the most excellent theory of time travel, poetic revolutions, and dynamic ...
On the most excellent theory of time travel, poetic revolutions, and dynamic ...
 
Perl Template Toolkit
Perl Template ToolkitPerl Template Toolkit
Perl Template Toolkit
 
Test Automatici^2 per applicazioni Web
Test Automatici^2 per applicazioni WebTest Automatici^2 per applicazioni Web
Test Automatici^2 per applicazioni Web
 
Hacking Movable Type
Hacking Movable TypeHacking Movable Type
Hacking Movable Type
 
Perl, musica automagica
Perl, musica automagicaPerl, musica automagica
Perl, musica automagica
 
POE
POEPOE
POE
 
Scatole Nere
Scatole NereScatole Nere
Scatole Nere
 

Perl101