29 de diciembre de 2009

Tutorial sobre perl (...en construcción)


El lenguaje perl esta especialmente indicado para el manejo de ficheros y la depuración de texto. Según me ha comentado una mogo que está ahora progrmando con él tiene unas expresioner regulares muy potentes y sencillas.
... ya veremos.

COMADOS Y ESTRUCTURAS BÁSICAS DEL PERL

1) Para confirmar si esta instalado el intérprete perl:

# perl -v

This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
Copyright 1987-2007, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl".  If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.


2) Para ver la ruta de perl:
#which perl
/usr/bin/perl

los scripts de perl deben comenzar indicando la ruta del interprete:
#!/usr/bin/perl

3) Variables en perl:
  • Escalares "$":




    • $NUM = 4;
    • $TEXT = "texto";



  • Arrays "@":




    • @NUMEROS = (1,2,3,4,5);
    • @DIAS = ("lunes" , "martes" , "miercoles");



  • Hash "%":




    • %DIAS = ( l =>; "lunes" , m =>; "martes" , mi =>; "miercoles"



4) Argumentos en perl:
./perlscript.pl a b c
$ARGV [0]  = a
$ARGV [1]  = b
$ARGV [2]  = c
$#ARGV   = 2; indica el indice del último argumento.

5) condicional "if" , "else", "elsif":

#!/usr/bin/perl
if ($VAR == 10) {
print "Es igual a 10";
} 
if ($VAR > 10) {

print "Mayor de 10\n";
}
if ($VAR > 20){
print "Mayor de 20\n";
}
if ($VAR > 30){
print "Mayor de 30\n";
}
else {
print "Menor de 10";
}


Formas resumidas de if: 

if($VAR > 10) print "Mayor de 10";
ó 
print "Mayor de 10" if($VAR > 10);


6) Condicional "unless"
#!/usr/bin/perl
print "Hola" unless ($NOHOLA = 1);

7) Bucle "while":
#!/usr/bin/perl 
$T = 0;
while ($T < 5) {
print "Hola 5 veces\n";
$T = $T + 1;
}

8) Bucle "until":
#!/usr/bin/perl 
$T = 1;
until ($T > 5) {
print "Hola 5 veces\n";
$T = $T + 1;
}

9) Bucle "for":
#!/usr/bin/perl
for ($T = 0 ; $T < 5 ; $T = $T + 1){
print "Hola 5 veces\n";
}

10) Bucle "foreach":
#!/usr/bin/perl
@SEMANA=("lunes" , "martes" , "..." , "y domingo");
foreach  $DIA  (@SEMANA) {
print "$DIA\n";
}


11) Abriendo archivos:
open (FILE, "file.txt");    #en modo lectura.
open (FILE, ">file.txt");   #en modo escritura.
open (FILE, ">>file.txt");  #en modo actualización.

open (FILE, ">file.txt") or print "Sin acceso al archivo";

close (FILE);

12) Escribiendo en archivos (open, print, close):
#!/usr/bin/perl
open (FILE, ">>file.txt") or die "Privilegios insuficentes\n";
print FILE "Texto añadido al archivo file.txt";
close (FILE);

13) Leyendo archivos
Podemos copiar el contenido de un archivo a una variable array, cada línea es un elemento de la matriz:

#!/usr/bin/perl
open (FILE, "file.txt");
@VAR = <FILE>;
close (FILE);
print @VAR;

Haciendo un bucle con las filas del archivo:

#!/usr/bin/perl
open (FILE, "file.txt");
while ($LINEA = <FILE>) {
print $LINEA;
}
close (FILE);


14) Quitar espacios en blaco a avariable: =~ s/ //g
#!/usr/bin/perl
$FRASE = "Frase con esapacios";
print "$FRASE\n";
$FRASE =~ s/ //g;
print "$FRASE\n";

result:
Frase con esapacios
Fraseconesapacios

15)  Expresiones regulares en perl:

Pattern
Matches
^A
"A" at the beginning of a line
A$
"A" at the end of a line
A^
"A^" anywhere on a line
$A
"$A" anywhere on a line
^^
"^" at the beginning of a line
$$
"$" at the end of a line

Regular Expression
Matches
[]
The characters "[]"
[0]
The character "0"
[0-9]
Any number
[^0-9]
Any character other than a number
[-0-9]
Any number or a "-"
[0-9-]
Any number or a "-"
[^-0-9]
Any character except a number or a "-"
[]0-9]
Any number or a "]"
[0-9]]
Any number followed by a "]"
[0-9-z]
Any number,

or any character between "9" and "z".
[0-9\-a\]]
Any number, or

a "-", a "a", or a "]"

Regular Expression
Matches
_
*
Any line with an asterisk
\*
Any line with an asterisk
\\
Any line with a backslash
^*
Any line starting with an asterisk
^A*
Any line
^A\*
Any line starting with an "A*"
^AA*
Any line if it starts with one "A"
^AA*B
Any line with one or more "A"'s followed by a "B"
^A\{4,8\}B
Any line starting with 4, 5, 6, 7 or 8 "A"'s

followed by a "B"
^A\{4,\}B
Any line starting with 4 or more "A"'s

followed by a "B"
^A\{4\}B
Any line starting with "AAAAB"
\{4,8\}
Any line with "{4,8}"
A{4,8}
Any line with "A{4,8}"

Regular Expression
Class
Type
Meaning
_
.
all
Character Set
A single character (except newline)
^
all
Anchor
Beginning of line
$
all
Anchor
End of line
[...]
all
Character Set
Range of characters
*
all
Modifier
zero or more duplicates
\<
Basic
Anchor
Beginning of word
\>
Basic
Anchor
End of word
\(..\)
Basic
Backreference
Remembers pattern
\1..\9
Basic
Reference
Recalls pattern
_+
Extended
Modifier
One or more duplicates
?
Extended
Modifier
Zero or one duplicate
\{M,N\}
Extended
Modifier
M to N Duplicates
(...|...)
Extended
Anchor
Shows alteration
_
\(...\|...\)
EMACS
Anchor
Shows alteration
\w
EMACS
Character set
Matches a letter in a word
\W
EMACS
Character set
Opposite of \w

Regular Expression
Class
Type
Meaning
\t
Character Set
tab
\n
Character Set
newline
\r
Character Set
return
\f
Character Set
form
\a
Character Set
alarm
\e
Character Set
escape
\033
Character Set
octal
\x1B
Character Set
hex
\c[
Character Set
control
\l
Character Set
lowercase
\u
Character Set
uppercase
\L
Character Set
lowercase
\U
Character Set
uppercase
\E
Character Set
end
\Q
Character Set
quote
\w
Character Set
Match a "word" character
\W
Character Set
Match a non-word character
\s
Character Set
Match a whitespace character
\S
Character Set
Match a non-whitespace character
\d
Character Set
Match a digit character
\D
Character Set
Match a non-digit character
\b
Anchor
Match a word boundary
\B
Anchor
Match a non-(word boundary)
\A
Anchor
Match only at beginning of string
\Z
Anchor
Match only at EOS, or before newline
\z
Anchor
Match only at end of string
\G
Anchor
Match only where previous m//g left off

http://www.grymoire.com/Unix/Regular.html

100) Bibliografía:
htttp://www.perl.org
Web oficial de perl.


No hay comentarios:

Publicar un comentario