SlideShare a Scribd company logo
1 of 169
Download to read offline
Corso
introduttivo a
Perl
Torino, 7 Settembre 2011
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
• OOP
• CPAN
Perl
~1987, Larry Wall
Perl 4.0
1991, Camel Book
Perl 5
1994, Perl come lo conosciamo ora
Perl 5.10
2008
Perl 5.16
Perl 6
“Quando arriva arriva”
Rakudo
Rakudo *
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
Bibliografia ::
Bibliografia ::
Programming Perl
Larry Wall et al.
Oreilly, 1995
Bibliografia ::
Modern Perl
chromatic
Onyx Neon, 2010
http://onyxneon.com/books/modern_perl/
Bibliografia ::
Pocket Perl
Stefano Rodighiero
Apogeo, 2008
Variabili ::
$
Scalari
Un valore singolo
Numeri e stringhe di caratteri
@
Array
Un insieme ordinato di valori scalari
%
Hash
Un insieme (non ordinato)
di coppie chiave-valore scalare
Fondamentale!
Documentazione
perldata
perlop
 Esercizi
https://github.com/larsen/perl101
Riferimenti e
strutture dati ::
Riferimenti e
strutture dati ::
Una questione in sospeso...
Riferimenti e
strutture dati ::
Una questione in sospeso...
use feature 'say';

# É 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
Documentazione
perlref
perlreftut
perldsc
perllol
 Esercizi
https://github.com/larsen/perl101
Subroutine ::
Subroutine ::
Codice riusabile (Don't repeat yourself: DRY)
Strumento di astrazione
Configurabilità del comportamento
Documentazione
perlsub
 Esercizi
https://github.com/larsen/perl101
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
map / grep
map BLOCK LIST
map EXPR, LIST
map BLOCK LIST
map EXPR, LIST
map BLOCK LIST
map BLOCK LIST
                                        n
                         K per  ciascu
              e BLOC                   ituisce
       Esegu                  , e rest
                   d i LIST
         le mento                  ita dal
       e                  costitu         uzione
                a lista
        un'altr                  na esec
                       i ciascu
        risu  ltato d
map EXPR, LIST
                                        lemento
                                scun e
                       per cia
       Valut  a EXPR                  n'altra
                      restit uisce u          i
       di LI  ST, e                   ltato d
                              al risu
                   t ituita d
        lis ta cos
                          tazione
                 na valu
         ciascu
grep BLOCK LIST
grep EXPR, LIST
grep BLOCK LIST
grep EXPR, LIST
                                                 n
                                       r ciascu
                       K o E XPR pe           e  un'altra
                    C
     Valu  ta BLO                 es  tituisc
                  d i LI  ST, e r            di LIS
                                                      T
     ele mento                    e lementi
                   it ui ta dagli              tuito un
      list a cost                    h a resti
                          tazione
               i la valu
       per cu
                      .
        valore vero
Documentazione
perlsyn
 Esercizi
https://github.com/larsen/perl101
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
                  filehand
close $fh;
Documentazione
perlfunc (perldoc -f ...)
perlopentut
 Esercizi
https://github.com/larsen/perl101
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
Documentazione
perlre
perlreref
 Esercizi
https://github.com/larsen/perl101
Moduli ::
Insieme alle
subroutine,
ulteriore
strumento di
astrazione
Documentazione
perlmod
 Esercizi
https://github.com/larsen/perl101
Ulteriori cenni
su strict
Quando è che
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 use
vars o con our.
OOP ::
Inizialmente
non integrata
nel linguaggio.
Perl 5
Oggetti (package, bless, ...)
1. Una classe è
un package
2. Un metodo è
una subroutine
3. Un oggetto è
un riferimento
blessed
2000
Damian Conway, «Object Oriented Perl» –
Manning
2006
Moose
Moose::
Modello
analogo a
quello di Perl 6
Implementato
in Perl 5
Documentazione
perlobj
perlboot
perltoot
perltooc
perlbot
 Esercizi
https://github.com/larsen/perl101
CPAN ::
search.cpan.org
$ perl -MCPAN -e shell
$ cpan
Qualche modulo
utile
DBI
DBIx::Class
Template
Getopt::Long
DateTime
Regexp::Common
HTML::Parser::*
XML::LibXML
Catalyst
Dancer
Mojolicious

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Perl101 - Italian Perl Workshop 2011