Corso di introduzione a Perl, Bologna 2012

Post on 29-Nov-2014

701 views 2 download

description

 

Transcript of Corso di introduzione a Perl, Bologna 2012

Corso introduttivo aPerlBologna, 10 Ottobre 2012

giovedì 11 ottobre 2012

Corso introduttivo aPerlStefano Rodighierohttp://www.stefanorodighiero.netstefano.rodighiero@gmail.com@larsen

giovedì 11 ottobre 2012

Cosa poteteaspettarvi daquesto corsoSapere scrivere dei semplici programmi PerlSapere leggere programmi PerlSapere dove trovare altra documentazione

giovedì 11 ottobre 2012

Corso introduttivo aPerl• Variabili•Riferimenti e strutture dati• Subroutine• I/O• Espressioni regolari•Moduli•OOP•CPAN

giovedì 11 ottobre 2012

Perl~1987, Larry Wall

giovedì 11 ottobre 2012

Perl 4.01991, Camel Book

giovedì 11 ottobre 2012

Perl 51994, Perl come lo conosciamo ora

giovedì 11 ottobre 2012

Perl 5.102008

giovedì 11 ottobre 2012

Perl 5.16

giovedì 11 ottobre 2012

Perl 6“Quando arriva arriva”

giovedì 11 ottobre 2012

Rakudo

giovedì 11 ottobre 2012

Rakudo *2010

giovedì 11 ottobre 2012

Perl

giovedì 11 ottobre 2012

PERLPractical Extraction and Report LanguagePathologically Eclectic Rubbish ListerPolymorphic Existential Recursive Lambdas

giovedì 11 ottobre 2012

PerlTIMTOWTDIRendere facili le cose facili, possibili le cose difficiliSimile ad un linguaggio naturale

giovedì 11 ottobre 2012

ProcurarsiPerlSe usate un sistema *NIX, molto probabilmente lo avete già.Per Windows, ActiveState Perl, Strawberry Perl, oppure DWIM Perl (basato su Strawberry Perl).

giovedì 11 ottobre 2012

Come cercare aiuto

giovedì 11 ottobre 2012

perldoc

giovedì 11 ottobre 2012

mongers@perl.it

giovedì 11 ottobre 2012

#perl.it

giovedì 11 ottobre 2012

www.perlmonks.org

giovedì 11 ottobre 2012

Bibliografia ::

giovedì 11 ottobre 2012

Bibliografia ::Programming PerlLarry Wall et al.Oreilly, 2012

giovedì 11 ottobre 2012

Bibliografia ::Modern PerlchromaticOnyx Neon, 2010http://onyxneon.com/books/modern_perl/In italiano: http://www.perl.it/documenti/modern_perl.html

giovedì 11 ottobre 2012

Bibliografia ::Pocket PerlStefano RodighieroApogeo, 2008

giovedì 11 ottobre 2012

Variabili ::

giovedì 11 ottobre 2012

$

giovedì 11 ottobre 2012

ScalariUn valore singoloNumeri e stringhe di caratteri

giovedì 11 ottobre 2012

@

giovedì 11 ottobre 2012

ArrayUn insieme ordinato di valori scalari

giovedì 11 ottobre 2012

%

giovedì 11 ottobre 2012

HashUn insieme (non ordinato)di coppie chiave-valore scalareFondamentale!

giovedì 11 ottobre 2012

Documentazioneperldataperlop

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Riferimenti e strutture dati ::

giovedì 11 ottobre 2012

Riferimenti e strutture dati ::Una questione in sospeso...

giovedì 11 ottobre 2012

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...

giovedì 11 ottobre 2012

Uno scalare può contenere:

giovedì 11 ottobre 2012

Un numero.

giovedì 11 ottobre 2012

Una stringa.

giovedì 11 ottobre 2012

Un riferimentoad un'altravariabile.

giovedì 11 ottobre 2012

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

giovedì 11 ottobre 2012

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

giovedì 11 ottobre 2012

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

@array

1

2

3

4

5

6

giovedì 11 ottobre 2012

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

4

5

6

1

2

3

@array

giovedì 11 ottobre 2012

1

2

3

@array

4

5

6

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

giovedì 11 ottobre 2012

1

2

3

@array

4

5

6

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

giovedì 11 ottobre 2012

my @array = ( {nome => "Pinco", cognome => "Pallino" }, {nome => "Lorenzo", cognome => "De' Medici"});

nomecognome

@array

nomecognome

PincoPallino

LorenzoDe' Medici

giovedì 11 ottobre 2012

my @array = ( {nome => "Pinco", cognome => "Pallino" }, { nome => "Lorenzo", cognome => "De' Medici", longevita => [1449, 1492] });

nomecognome

@array

nomecognome

PincoPallino

LorenzoDe' Medici

longevità •14491492

giovedì 11 ottobre 2012

my $nome = $array[0]->{nome};

nomecognome

@array

nomecognome

PincoPallino

LorenzoDe' Medici

longevità •14491492

giovedì 11 ottobre 2012

my $anno_morte_ldm = $array[1]->{longevita}->[1];

nomecognome

@array

nomecognome

PincoPallino

LorenzoDe' Medici

longevità •14491492

giovedì 11 ottobre 2012

Documentazioneperlrefperlreftutperldscperllol

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Subroutine ::

giovedì 11 ottobre 2012

Subroutine ::Codice riusabile (Don't repeat yourself: DRY)Strumento di astrazioneConfigurabilità del comportamento

giovedì 11 ottobre 2012

Documentazioneperlsub

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Controllodel flusso ::

giovedì 11 ottobre 2012

Esecuzionecondizionale ::

giovedì 11 ottobre 2012

if ... elsif ... else

giovedì 11 ottobre 2012

if (EXPR) BLOCK

giovedì 11 ottobre 2012

if (EXPR) BLOCKUn'espre

ssione,

che viene valu

tata

in contesto

booleano.

giovedì 11 ottobre 2012

Un'espressione

,

che viene valu

tata

in contesto

booleano.

Falso0 (il numero zero)'' (la stringa vuota)'0' (una stringa che contiene il singolocarattere corrispondente alla cifra zero)undef

giovedì 11 ottobre 2012

Un'espressione

,

che viene valu

tata

in contesto

booleano.

VeroTutto il resto

giovedì 11 ottobre 2012

if (EXPR) BLOCKUn'espre

ssione,

che viene valu

tata

in contesto

booleano.

Un insieme diistruzioni, delimitateda graffe.giovedì 11 ottobre 2012

if (EXPR) BLOCK

giovedì 11 ottobre 2012

if (EXPR) BLOCKelse BLOCK

giovedì 11 ottobre 2012

if (EXPR) BLOCKelsif (EXPR) BLOCKelse BLOCK

giovedì 11 ottobre 2012

if (EXPR) BLOCKelsif (EXPR) BLOCKelse BLOCK

giovedì 11 ottobre 2012

given ... when

giovedì 11 ottobre 2012

Come switch e case in altrilinguaggi

giovedì 11 ottobre 2012

given (EXPR) { when (EXPR) BLOCK ... default BLOCK}

giovedì 11 ottobre 2012

given (EXPR) { when (EXPR) BLOCK ... default BLOCK}

giovedì 11 ottobre 2012

given (EXPR) { when (EXPR) BLOCK ... default BLOCK}

giovedì 11 ottobre 2012

given (EXPR) { when (EXPR) BLOCK ... default BLOCK}

giovedì 11 ottobre 2012

Modificatoridi statement ::

giovedì 11 ottobre 2012

... if EXPR;

... unless EXPR;

... while EXPR;

... until EXPR;

... foreach EXPR;

giovedì 11 ottobre 2012

Iterazione ::

giovedì 11 ottobre 2012

for / foreach

giovedì 11 ottobre 2012

foreach (LIST) BLOCK

giovedì 11 ottobre 2012

foreach (LIST) BLOCK

giovedì 11 ottobre 2012

foreach (LIST) BLOCK

La lista dei v

alori

sui quali ciclare

giovedì 11 ottobre 2012

foreach (LIST) BLOCK

La lista dei v

alori

sui quali ciclare

Un insieme diistruzioni, delimitateda graffe.

giovedì 11 ottobre 2012

foreach (LIST) BLOCK

La lista dei v

alori

sui quali ciclare

Un insieme diistruzioni, delimitateda graffe.All'interno del blocco, il singolo elemento è $_

giovedì 11 ottobre 2012

foreach my $var (LIST) BLOCK

giovedì 11 ottobre 2012

All'interno del blocco è tipicousare ulteriori istruzioni per il controllo di flusso

giovedì 11 ottobre 2012

next

giovedì 11 ottobre 2012

redo

giovedì 11 ottobre 2012

last

giovedì 11 ottobre 2012

while

giovedì 11 ottobre 2012

while (EXPR) BLOCK

giovedì 11 ottobre 2012

while (EXPR) BLOCK

L'espression

e

viene valutata in

contesto

booleano

giovedì 11 ottobre 2012

while (EXPR) BLOCK

L'espression

e

viene valutata in

contesto

booleano

Il blocco viene eseguito finchè la condizione è veragiovedì 11 ottobre 2012

map / grep

giovedì 11 ottobre 2012

map BLOCK LISTmap EXPR, LIST

giovedì 11 ottobre 2012

map BLOCK LISTmap EXPR, LIST

giovedì 11 ottobre 2012

map BLOCK LIST

giovedì 11 ottobre 2012

map BLOCK LISTEsegue BLOCK per ciasc

un

elemento di LIST, e restituisce

un'altra lista costituita dal

risultato di ciascuna esecuzione

giovedì 11 ottobre 2012

map EXPR, LISTValuta EXPR per ciascu

n elemento

di LIST, e restituisce un'altra

lista costituita dal risu

ltato di

ciascuna valutazione

giovedì 11 ottobre 2012

grep BLOCK LISTgrep EXPR, LIST

giovedì 11 ottobre 2012

grep BLOCK LISTgrep EXPR, LIST

Valuta BLOCK o EXPR per ciascun

elemento di LIST, e restituisce un'altra

lista costituita dagli ele

menti di LIST

per cui la valutazione ha restituito un

valore vero.

giovedì 11 ottobre 2012

Documentazioneperlsyn

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

I/O ::

giovedì 11 ottobre 2012

Lettura da file

giovedì 11 ottobre 2012

open my $fh, "<", $filename;

giovedì 11 ottobre 2012

open my $fh, "<", $filename;

Il filehandle

giovedì 11 ottobre 2012

open my $fh, "<", $filename;

Il filehandle

In che modalità

aprire il file?

giovedì 11 ottobre 2012

open my $fh, "<", $filename;

Il filehandle

In che modalità

aprire il file?

Il nom

e del

file

giovedì 11 ottobre 2012

Modalità di apertura

giovedì 11 ottobre 2012

< Lettura

> Scrittura

>> Append

+< Read/write

+> Write/read (se il file esiste lo tronca)

giovedì 11 ottobre 2012

my $line = <$fh>;

giovedì 11 ottobre 2012

my $line = <>;

Forma abbreviata,

legge da STDIN

giovedì 11 ottobre 2012

my $line = <DATA>;

Forma speciale,

legge dal

filehandle DATA

giovedì 11 ottobre 2012

close $fh;

giovedì 11 ottobre 2012

Documentazioneperlfunc (perldoc -f ...)perlopentut

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Espressioniregolari ::

giovedì 11 ottobre 2012

Un linguaggio dentro il linguaggio.

giovedì 11 ottobre 2012

Espressioni regolariUna maniera per esprimere insiemi di stringhe di caratteriRiconoscimento di formatiManipolazione del testo

giovedì 11 ottobre 2012

/foo/

giovedì 11 ottobre 2012

foo

giovedì 11 ottobre 2012

foo bar foo baz

giovedì 11 ottobre 2012

bar foo bazfoo

giovedì 11 ottobre 2012

bar foo bazfoo

giovedì 11 ottobre 2012

bar foo bazfoo

giovedì 11 ottobre 2012

bar foo bazfoo

giovedì 11 ottobre 2012

bar foo bazfoo

giovedì 11 ottobre 2012

Documentazioneperlreperlreref

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Moduli ::

giovedì 11 ottobre 2012

Insieme alle subroutine, ulteriore strumento di astrazione

giovedì 11 ottobre 2012

Documentazioneperlmod

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

Ulteriori cenni su strict

giovedì 11 ottobre 2012

Quando è che strict si lamenta per le variabili?

giovedì 11 ottobre 2012

1.

giovedì 11 ottobre 2012

La variabile non è completamente qualificata.

giovedì 11 ottobre 2012

2.

giovedì 11 ottobre 2012

Non è una variabile lessicale precedentemente definita.

giovedì 11 ottobre 2012

3.

giovedì 11 ottobre 2012

Non è una variabile package precedentemente definita con use vars o con our.

giovedì 11 ottobre 2012

OOP ::

giovedì 11 ottobre 2012

Inizialmente non integrata nel linguaggio.

giovedì 11 ottobre 2012

Perl 5Oggetti (package, bless, ...)

giovedì 11 ottobre 2012

1. Una classe è un package

giovedì 11 ottobre 2012

2. Un metodo è una subroutine

giovedì 11 ottobre 2012

3. Un oggetto è un riferimento blessed

giovedì 11 ottobre 2012

2000Damian Conway, «Object Oriented Perl» – Manning

giovedì 11 ottobre 2012

2006Moose

giovedì 11 ottobre 2012

Moose::

giovedì 11 ottobre 2012

Modello analogo a quello di Perl 6

giovedì 11 ottobre 2012

Implementato in Perl 5

giovedì 11 ottobre 2012

Documentazioneperlobjperlbootperltootperltoocperlbot

giovedì 11 ottobre 2012

Esercizi

https://github.com/larsen/perl101

giovedì 11 ottobre 2012

CPAN ::

giovedì 11 ottobre 2012

search.cpan.org

giovedì 11 ottobre 2012

$ perl -MCPAN -e shell

giovedì 11 ottobre 2012

$ cpan

giovedì 11 ottobre 2012

Qualche modulo utile

giovedì 11 ottobre 2012

DBI

giovedì 11 ottobre 2012

DBIx::Class

giovedì 11 ottobre 2012

Template

giovedì 11 ottobre 2012

Getopt::Long

giovedì 11 ottobre 2012

DateTime

giovedì 11 ottobre 2012

Regexp::Common

giovedì 11 ottobre 2012

HTML::Parser::*

giovedì 11 ottobre 2012

XML::LibXML

giovedì 11 ottobre 2012

Catalyst

giovedì 11 ottobre 2012

Mojolicious

giovedì 11 ottobre 2012

Dancer

giovedì 11 ottobre 2012