![Page 2: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/2.jpg)
Roadmap
• O que é o Perl
• Perl para iniciantes– Programa “Hello World”– Tipos de dados
• TIMTOWDI– Hello World– Estruturas de controlo– Regex (Regular Expressions)– Funções
![Page 3: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/3.jpg)
O que é o Perl
• Linguagem de programação estável e multiplataforma
• Criada em 1987 por Larry Wall
• Especialmente eficaz no tratamento de Strings
• Permite tempos de desenvolvimento curtos
• Lema do Perl: – There Is More Than One Way to Do It
(TIMTOWDI)
![Page 4: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/4.jpg)
Perl para iniciantes
![Page 5: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/5.jpg)
Hello World
• Código
print 'Hello World!'
• Resultado– Hello World!
![Page 6: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/6.jpg)
IDE
• Eclipse Epic
![Page 7: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/7.jpg)
Tipos de dados
• 3 tipos de variaveis– Escalares
• my $scalar;
– Arrays• my @array;
– Hashes• my %hash;
![Page 8: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/8.jpg)
Escalares
• Códigomy $str;my $num;$str = 'Tomar.pm';$num = 1;print 'O grupo ',$str, ' é o numero ',
$num;
• Output– O grupo Tomar.pm é o numero 1
![Page 9: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/9.jpg)
Arrays
• Códigomy @array;$array[0] = 'Tomar.pm';$array[1] = 1;
print 'O grupo', $array[0],' é o numero ', $array[1];
• Output– O grupo Tomar.pm é o numero 1
![Page 10: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/10.jpg)
Hashes
• Códigomy %hash;$hash{nome} = 'Tomar.pm';$hash{num} = 1;
print 'O grupo ',$hash{nome},' é o numero ',$hash{num};
• Output– O grupo Tomar.pm é o numero 1
![Page 11: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/11.jpg)
O valor undef
• Códigomy $value;if (defined $value) {
print 'está definido';}else {
print 'indefinido';}
• Output– indefinido
![Page 12: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/12.jpg)
TIMTOWDI
![Page 13: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/13.jpg)
Hello World
• Na consola:perl -e 'print "Hello World!!\n"'
• Resultado– Hello World!!
• Conhecido como “one liners”
![Page 14: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/14.jpg)
Hello World
• Ficheiro helloWorld.pl:– Código
print 'Hello World!!”;
• Executar o programaperl helloWorld.pl
• Resultado– Hello World!!
![Page 15: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/15.jpg)
Hello World
• Código mais comum:#!/usr/bin/perl
print “Hello World!!\n”;
• Executarchmod +x helloWorld.pl./helloWorld.pl
![Page 16: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/16.jpg)
Estruturas de controlo
• If ... elsif .. elseif ($esta_a_nevar) {
print “leva a samarra”;}elsif ($esta_a_chover) {
print “leva o guarda chuva”;else {
print “vai de calcoes e t-shirt”;}
![Page 17: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/17.jpg)
Estruturas de controlo
• Comando forfor (my $i=1; $i<10; $i++) {
print $i;}
![Page 18: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/18.jpg)
Estruturas de controlo
• Comando formy @nums = 1..10;for my $i (@nums) {
print $i;}
![Page 19: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/19.jpg)
Estruturas de controlo
• Comando formy @nums = 1..10;for (@nums) {
print $_; # igual a print;}
![Page 20: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/20.jpg)
Estruturas de controlo
• Comando whilemy $counter = 1;while ( 1 ) {
print "Estou em loop infinito";$counter++;if ($counter==6) {
last; # last = java break}
}print 'o contador esta com ', $counter;
• Output– o contador esta com 6
![Page 21: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/21.jpg)
Estruturas de controlo
• Comando whilemy $counter = 1;while ( 1 ) {
print "Estou em loop infinito";last if ++$counter == 6;
}print "o contador esta com ", $counter;
• Output– o contador esta com 6
![Page 22: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/22.jpg)
Estruturas de controlo
• Modificadores de espressõesif ($temp > 20) {
print “Esta calor”;}
• Em vez disso:print “Esta calor” if ($temp > 20);
![Page 23: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/23.jpg)
Estruturas de controlo
• Modificadores de expressão
print “Esta calor if ($temp > 20);print “$_\n” for (1..10);print “$i++\n” while ($i<10);
![Page 24: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/24.jpg)
Estruturas de controlo
• Operador ternário($num == 1) ? print “E igual a 1”
: print “Nao e igual a 1”;
• Outro exemplo:my $size =
($width < 10) ? 'small' :($width < 20) ? 'medium' :($width < 50) ? 'large' :
'extra-large'; #default
![Page 25: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/25.jpg)
Estruturas de controlo
• Comparação com if .. elsif .. elseif ($width < 10)
$size = “small”;} elsif ($width < 20) {
$size = “medium”;} elsif ....
} else {$size = “extra-large”;
}
![Page 26: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/26.jpg)
Regex
• Código:my $texto = 'Ola mundo';print ($texto =~ /ola/);
• Resultado– 1
![Page 27: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/27.jpg)
Regex
• Códigomy $texto = 'Ola mundo';$texto =~ s/mundo/IPT/;
print $texto;
• Resultado– Ola IPT
![Page 28: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/28.jpg)
Funções
• Código&func('ola ', 'mundo');sub func {
my ($param1, $param2) = @_;print “$param1 $param2”;
}
• Outputola mundo
![Page 29: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/29.jpg)
Referências
• Randal Schwartz, Tom Phoenix, brian d foy,Learning Perl, 4ª edição, O'Reilly, 2005
![Page 30: Introdução ao Perl Apresentado por: Mário Silva marricardo@gmail](https://reader035.vdocuments.com.br/reader035/viewer/2022062722/56813ae8550346895da34d9b/html5/thumbnails/30.jpg)
Dúvidas