Programando em Perl: Nelson Corrêa de Toledo Ferraz
Programando em Perl: Nelson Corrêa de Toledo Ferraz
X
($a)=@ARGV;$a||=10;$b=40;$d='.';$c=($b-$a);$f=($c/2);
$g=0;$j=5;$e=1;$h=$f;foreach(0..$c){$i[$_]=(100/$c)*$_}while($j){@k=();if
($h<$f){$h=$f;$j--;$e=-$e}elsif($h>=($f+$a)){$h=$f+$a-1;$j--;$e=-$e}$|
=1;@l=(' ')x$a;$l[$h-($f)]=$d;push @k,'*'x($f+1);push @k,@l;push @k,'*'x
($c-$f+1);push @k,' ',$j;print join('',(@k));if(rand(100)<=$i[$f]){$f--;}
else{$f++}$g++;$y=$x='';vec($x,fileno(STDIN),1)=1;if(select
($y=$x,undef,undef,.1)){until(sysread(STDIN,$b,1)){}$e=-$e;}else
{print"\n"}$h+=$e}print"$g\n";
#!/usr/bin/perl
# Primeiro programa
print "Hello,
world!
";
#!/usr/bin/perl -w
use strict;
#!/usr/bin/perl -w
use strict;
my $animal = "camel";
my $number = 42;
#!/usr/bin/perl -w
use strict;
my $x = 7;
my $y = "007";
if ($x == $y) {
print "'$x' é igual a '$y'!\n";
}
# Comparações numéricas
if ($idade == 18) { ... }
if ($idade > 100) { ... }
# Comparações literais
if ($resposta eq "s") { ... }
if ($nome ne "Larry") { ... }
foreach (@animals) {
print "$_\n";
}
my %fruit_color = (
apple => "red",
banana => "yellow",
);
my %color = (
apple => "red",
banana => "yellow",
);
sub square {
my $num = shift;
my $result = $num * $num;
return $result;
}
if (open(FILE,"filename.txt") {
# ...
} else {
print "Erro!";
}
close FILE;
@conteudo = <FILE>;
close FILE;
while (<>) {
# ...
}
foreach (@lang) {
print if m/P/;
}
while (<>) {
next if m/^#/g;
...
}
s/foo/bar/
foreach (@lang) {
s/P/J/;
print;
}
/item\d+/;
● Metacaracteres:
\ ignora o próximo metacaractere
^ início da linha
. qualquer caractere
$ final da linha
| alternação
() agrupamento
[] classe de caracteres
● Quantificadores:
* 0 ou mais vezes
+ 1 ou mais vezes
? 1 ou 0 vezes
{n} exatamente n vezes
{n,} pelo menos n vezes
{n,m} pelo menos n, no máximo m
Shell Perl
list.? ^list\..$
project.* ^project\..*$
*old ^.*old$
type*.[ch] ^type.*\.[ch]$
*.* ^.*\..*$
* ^.*$
package Portugues;
use Filter::Simple;
FILTER {
s/para cada /foreach /g;
s/escreva /print /g;
s/se /if /g;
s/encontrar /m/g; use Portugues;
s/substituir /s/g;
} escreva "Olá, mundo!\n";
use Portugues;
● use strict
● use warnings
● Comentários
● Maiúsculas e minúsculas
● Espaçamento vertical e horizontal
● Procure a forma mais legível
use strict;
$ALL_CAPS_HERE # constantes
$Some_Caps_Here # variáveis globais
$no_caps_here # variáveis locais
$IDX = $ST_MTIME;
$IDX = $ST_ATIME if $opt_u;
$IDX = $ST_CTIME if $opt_c;
$IDX = $ST_SIZE if $opt_s;
$ perldoc perlintro
$ perldoc perlvar
$ perldoc perldata
$ perldoc perlrequick
$ perldoc perlretut
● É simples!
– Coloque uma contrabarra ("\") antes do
nome da variável:
$scalar_ref = \$scalar;
$array_ref = \@array;
$hash_ref = \%hash;
$y = {
a => 1,
b => 2,
c => 3
};
$x = [ 1, 2, 3 ];
@x = @{$x};
$y = {
a => 1,
b => 2,
c => 3
};
%y = %{$y}
sub check_size {
my ($a1, $a2) = @_;
print @{$a1} == @{$a2} ? 'Yes' : 'No';
}
● Estruturas complexas:
my $variables = {
scalar => {
description => "single item",
sigil => '$',
},
array => {
description => "ordered list of items",
sigil => '@',
},
hash => {
description => "key/value pairs",
sigil => '%',
},
};
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA
Finland: Helsinki.
Germany: Berlin, Frankfurt.
USA: Chicago, New York, Washington.
$ perldoc perlref
$ perldoc perlreftut