Perl Basico
-
Upload
juniorsempre -
Category
Documents
-
view
49 -
download
0
Transcript of Perl Basico
VOLDAY
MINICURSO Hands on de Perl Básico
●Curso de C●Curso de Redes●Curso de Perl
ConsultoriaDesenvolvimentoTreinamentoInfraestrutura
Quem sou eu?
●Just another Perl hacker...●Tecnico Eletrônico e Telecom●Tecnólogo em Automação Industrial●Administrador de Redes e instrutor Petrobras●Instrutor de Perl, C e Redes na TICursos
●Curso Presencial: Petrobras e UTAH;
Desenvolvimento Unix
Os limites da minha linguagem são os limites do meu mundo.
“Ludwig Wittgenstein”
Escolha a Linguagem Correta
●A linguagem correta está além do mero gosto!●As vezes a melhor opção é um “mix” de
linguagens.
Introdução ao Perl
●Linguagem dinâmica;●Compilador e Interpretador;
●Tipos e$calar, @rrays e %hash●Objetos, filehandles;●Referências;●Funções, Módulos e Objetos.
Codigo e$calar
#!/usr/bin/perl#exemplo2.1
$numero1 = 5;$numero2 = 10;$resultado = $numero1 + $numero2;
print "$numero1 + $numero2 = $resultado\n";
Código Hashs
#!/usr/bin/perl#exemplo2.6 – uso dos hashs
%tabela_host = ( Servidor => '192.167.2.21',
Computador1 => '192.167.2.2',
Computador2 => '192.167.2.3',
Roteador => '192.167.2.254');
print "$tabela_host{Servidor}\n";
print "$tabela_host{Roteador}\n";
Código @rray:
#!/usr/bin/perl
#exemplo2.5 – uso de arrays
@meus_sonhos = ('casa', 'carro', 'apartamento');
@meus_numeros = (1, 2, 3, 4, 10, 100);
print "meus números são @meus_numeros\n";
print "$meus_sonhos[2] vale $meus_numeros[5]\n";
print "Soma de $meus_numeros[2] com $meus_numeros[4] é: ", $meus_numeros[2] + $meus_numeros[4];
Loops
●for, ●foreach,● while, do{...}while,● map grep, sort, ~~
Código
●my @array = (0..9);●my @grep = grep {$_ > 5} @array;●say "@grep";●
Loop
●for (keys %ENV){● say "$_ => $ENV{$_}"●}●
Decisão
●if, elsif, else;● unless;● given( $var ){
when (EXP1) { codigo }when (EXP2) {codigo}default {codigo}}
Código:
use 5.10.0;use Filehandle;say "Digite o nome e caminho de um arquivo:";chomp (my $path = <STDIN>);my ($char, $digito, $linha) = (0,0,0,0);my $file = FileHandle->new($path, 'r') or die "Erro ao abrir arquivo: $!";while (defined (my $caractere = $file->getc())){ given ($caractere){ when (/\d/) {$digito++; $char++} when ($_ eq "\n") { $linha++; $char++ } default {$char++} }}printf "O arquivo tem %d linhas, %d digitos e %d caracteres", $linha, $digito,$char
Referências:
use 5.10.0; use strict;
use warnings;my @array = (1..10);
my %hash = (casa => 250_000,Carro => 30_000);
my $ar_ref = \@array;my $hash_ref = \%hash;
say $ar_ref;Say hash_ref;
OOP.
Mostrar Outro Slide.
CGI
●Crie protótipos de aplicações com CGI.pm● Código legível para programadores● Fácil de manter e testar● Pode ser usado com paradigma OOP ou
orientado a funções●O CGI ainda é uma opção para pequenas aplicações Web e aplicações intranet sem muito tráfego.
Hellocgi.pl● #!/usr/bin/perl -T● #@AUTOR=Thiago Glauco Sanchez● #Empresa= TICursos.net● #Perl just Rocks...●
● use 5.10.0;● use strict;● use warnings;● use CGI;●
● #Warnings - Developement only● use CGI::Carp qw/fatalsToBrowser/;●
● #medir desempenho● use Benchmark;●
● my $start_time = Benchmark->new();●
● #Proteção contra DOS.● $CGI::DISABLE_UPLOADS = 1;● $CGI::POST_MAX = 102_400; #100K●
● my $cgi = CGI->new();● print $cgi->header("text/html; charset=iso-8859-1"),● $cgi->start_html(-title => "TICursos and Perl"),● $cgi->body(),● $cgi->h2("TICursos, Rocking whith Perl"),● $cgi->hr;● print $cgi->h3("Este eh o processo ID $$");● my $end_time = Benchmark->new();● print $cgi->h2("Executado em" . timestr(timediff($end_time, $start_time) )),● $cgi->end_html;
simple_form.pl
● #!/usr/bin/perl -T● #@AUTOR=Thiago Glauco Sanchez● #Empresa= TICursos.net● #Perl just Rocks...●
● use 5.10.0;● use strict;● use warnings;● use CGI;●
● #Warnings - Developement only● use CGI::Carp qw/fatalsToBrowser/;●
● #medir desempenho● use Benchmark;●
● my $start_time = Benchmark->new();●
● #Proteção contra DOS.● $CGI::DISABLE_UPLOADS = 1;● $CGI::POST_MAX = 102_400; #100K●
● #inicia o html● my $cgi = CGI->new();● print $cgi->header("text/html; charset=iso-8859-1"),● $cgi->start_html(-title => "TICursos and Perl"),● $cgi->body(),● $cgi->h2("TICursos, Rocking whith Perl"),● $cgi->hr;●
● #Iniciando o Form;● print $cgi->start_form(-action => $cgi->url),● "Digite seu nome: ", $cgi->textfield(-name => "nome"), $cgi->br,● "Digite sua idade: ", $cgi->textfield(-name => "idade"), $cgi->br,● "Digite seu e-mail: ", $cgi->textfield(-name => "mail"),$cgi->br,● $cgi->reset, "\t", $cgi->submit(-name => "enviado"),● $cgi->end_form, $cgi->hr;●
● my $end_time = Benchmark->new();● print $cgi->h3(timestr(timediff($end_time, $start_time)));● print $cgi->end_html;
Melhoras na programação Web
●Use Ferramentas de Template;– TemplateToolkit– Manson– Apache::Asp– Embperl– Html::Template
●mod_fcgi e mod_perl para velocidade;
Aplicação Desktop:
●Perl/Tk– Rápido– Fácil– Bem Documentado.
●UI::Dialog●UI::Dialog::Console
– Fácil– Rápido– Pouco elegante
UI::Dialog
●UI::Dialog::Console– use UI::Dialog::Console;– my $d = new UI::Dialog::Console ( title => 'Default',– height => 20, width => 65,– listheight => 5 );–
–
– $d->msgbox( title => 'Bem Vindo!', text => 'Tenha um VolDay!!!' );●
●UI::Dialog– use UI::Dialog;– my $d = new UI::Dialog ( title => 'Default',– height => 20, width => 65,– listheight => 5 );–
–
– $d->msgbox( title => 'Bem Vindo!', text => 'Tenha um VolDay!!!' );
HelloTk.pl
●Tk:use 5.10.0;use strict;use warnings;use Tk;
my $mw = MainWindow->new;
$mw->title("Ola!!!");my $label = $mw->Label(-text => "Bem vindo a programacao
Desktop com Perl");$label->pack;my $button = $mw->Button(-text => "sair", -command => sub{exit
0});$button->pack;MainLoop;
OBRIGADO
●www.vivaolinux.com.br●www.ticursos.net●Luciana●sao-paulo.pm