Thursday, November 3, 2011

Perl - [ Reference | Array Sub ]


#!/usr/bin/perl
use strict;
use warnings;

my @array = [1,2,3,['a','b',['c']]];
my $rA = \@array;

print"\n[0] = [1,2,3,['a','b',['c']]]";
print"\n[3] = ['a','b',['c']]";
print"\n[2] = ['c']";
print"\n[0] = c";

print "\nResp : $rA->[0][3][2][0]\n";

my @des = [ ['andre','21','PA'],
            ['ela','-','SP']];

my $rD = \@des;
    print $rD->[0][0][0];

result


$ perl line.pl

[0] = [1,2,3,['a','b',['c']]]
[3] = ['a','b',['c']]
[2] = ['c']
[0] = c
Resp : c
andre


Perl - [ Captura Regex ]

#!/usr/bin/perl

use strict;
use warnings;

my $capitura = 'From: gnat@perl.com To: camelo@oreilly.com Date: Mon 17 Jul 200 09:00:00 -100 Subject: Nada';
$capitura =~ /(From:(.*)To:(.*)\s{1}Date:(.*)\s{1}Subject:(.*))/;
print "$1\n$2\n$3\n$4\n$5";

result


$perl  commandLine.pl
$1  # From: gnat@perl.com To: camelo@oreilly.com Date: Mon 17 Jul 200 09:00:00 -100 Subject: Nada
$2  # gnat@perl.com
$3  # camelo@oreilly.com
$4  # Mon 17 Jul 200 09:00:00 -100
$5  # Nada


Perl - [Character class | Change | Replacement | For ]


#!/usr/bin/perl
use strict;
use warnings;

my $name = ' texto gerado automaticamente pelo sistema lattes ';
my $name1;

($name1 = $name) =~ s/o/OoO/g;

print "$name\n$name1\n";

for ($name){
    s/^\s+/+/;
    s/\s+$/++/;
    s/\s+/-/g;
}
print "$name\n";

my @names = ('comand','   ','line');

print "@names\n";

tr/ /./ for @names;

print "@names\n";

my ($num,$string,$space) = (10,'line',' ');

print "numero positive - $num \n"   if ($num =~ /\d/);
print "string positive - $string \n"    if ($string =~ /\w/);
print "space  positive - $space \n" if ($space =~ /\s/);

print "numero positive - $num \n"   if ($num =~ /\p{IsDigit}/);
print "string positive - $string \n"    if ($string =~ /\p{IsWord}/);
print "space  positive - $space \n" if ($space =~ /\p{IsSpace}/);

print "numero negation - $num \n"   if ($num =~ /\D/);
print "string negation - $string \n"    if ($string =~ /\W/);
print "space  negation - $space \n" if ($space =~ /\S/);

print "numero negation - $num \n"   if ($num =~ /\P{IsDigit}/);
print "string negation - $string \n "if ($string =~ /\P{IsWord}/);
print "space  negation - $space \n" if ($space =~ /\P{IsSpace}/);

results 

$perl  blog.pl
 texto gerado automaticamente pelo sistema lattes 
 textOoO geradOoO autOoOmaticamente pelOoO sistema lattes 
+texto-gerado-automaticamente-pelo-sistema-lattes++
comand     line
comand ... line
numero positive - 10 
string positive - line 
space  positive -   
numero positive - 10 
string positive - line 
space  positive - 

Tuesday, November 1, 2011

Perl - [ Relational operators ]



numeric                     string
  >                                gt
  >=                             ge
  <                                lt
 <=                              le
  ==                             eq
  !=                              ne
<=>                             cmp

Friday, October 28, 2011

Perl - Pattern Matching [ Print`s ... ]

#!/usr/bin/perl
use strict;
use warnings;

my $text = "encrypted key\n";

if ($text =~  /key/){
    print qq/$text new print !\n/;
}
if(lc ($text) =~ /key/){
    print "$text";
}

$text = "/home /andre /work /tmp /bin";
if($text =~ m[/bin]){
    print "Directory /tmp found!\n";
}

$text = "andre luiz\n";
$text =~ s(andre)<AnDrE>;
$text =~ s[l]/L/;
$text =~ s{uiz}{UIZ};

print $text;

$text = "Andre Luiz Silva";
$text =~ /Luiz/;

print "$text\n ";
print  "left :  <$`> \n";
print  "center  : <$&> \n";
print  "right  : <$'> \n";

$text =~ "andre luiz silva\n";
if ($text =~ /Andre/){ print "$text\n";}
if ($text =~ m/Andre/){ print "m/$text\n";}

$text = "aaabbb";
$text =~ s/a/A/g;
print "$text\n";

$text = "aaabbb";
$text =~ s/a/A/;
print "$text\n";

$text = "aaabbb";
$text =~ tr/a/A/;
print "$text\n";

Result 

$ perl blog.pl

encrypted key
new print !
encrypted key
Directory /tmp found!
AnDrE LUIZ
Andre Luiz Silva
 left :  <Andre >
center  : <Luiz>
right  : < Silva>
Andre Luiz Silva
m/Andre Luiz Silva
AAAbbb
Aaabbb
AAAbbb