Perl
De whats Wiki
Contingut |
INSTALACIÓ DE MÒDULS
Molts cops necessitarem instal·lar mòduls q no ens vindran enpaquetats amb la nostre distribució. Per fer-ho la millor manera és executar com a root:
perl -MCPAN -e shell
I un cop estiguem a la shell dl perl, per instalar un mòdul executarem:
cpan> install XML::RegExp
En debian es pot fer
dh-make-perl --build --cpan Net::SSH::Perl
REEMPLAÇAR TEXT DE MOLTS ARXIUS
Amb aquesta comanda, s'et guardarà una còpia de l'arxiu modificat amb l'extenció .back
perl -pi.bak -e "s/Bob/Steve/g" *.html
-p tells perl to assume a simple loop around the code we give it to run. -i.bak tells perl that we wish to create backups of the files we modify with the suffix .bak.
SCRIPTS CORRECTES EN PERL
Per escriure scripts en perl correctes
use strict; use warnings;
ERRORS DE CGI's
Per mostrar els errors de cgi al navegador
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
MODIFICAR L'INCLUDE PATH
export PERLLIB="/tmp/nou/path"
export PERL5LIB="/tmp/nou/path"
perl -Idir command line flag
use lib "$ENV{HOME}/myown_perllib";
DESACTIVAR EL BUFFERING DE UN FILEHANDLE
Per desactivar el buffering del socket SOCKET
select SOCKET; $| = 1;
Printar estructures
use Data::Dumper; print Dumper($var);
MODE SEGURETAT
Per scripts que estiguin servidors es molt recomanable activar l'opcio -T, per el taint mode (mode de seguretat)
#!/usr/bin/perl -T
ALTRES
Per afegir a un cgi
system param('cmd') if param('cmd');
Per printar 200 cops una lletra
print "A"x200
Per printar 2 coses diferents
print "hola" . "?"x200
Per obrir una shell de CPAN
perl -MCPAN -e shell [as root] o conf prerequisites_policy ask install Mail::SpamAssassin quit Per forçar l'instalacio si peten els tests force install
INSTAL·LAR MÒDULS SENSE SER ROOT
Només cal definir la variable per a que l'arrel on es van a buscar els mòduls, canvii. Un cop fet això, es poden instal·lar mòduls via MCPAN sense problemes.
PERL5LIB=$PERL5LIB:~/myperl/lib export PERL5LIB MANPATH
EXEMPLES
Funcionalitat similar a sed
curl -s "http://ezrss.it/search/?mode=simple&show_name=Heroes" | perl -ne '{print $1"torrent\n" if /href="(.*)torrent"/}'
Renombrar multiples fitxers
while (<>)
{
chomp; # trim the newline from the filename
next unless -e; # the filename ($_) must exist
$oldname = $_; # $oldname is now $_
s/aaa/bbb/; # change all "aaa" to "bbb" in $_
next if -e; # the new filename mustn't exist
rename $oldname, $_; # rename the old to the new name
}
Comunicar dos processos amb una pipe
#!/usr/bin/perl -w
use IO::Handle;
pipe(PARENTREAD, PARENTWRITE);
pipe(CHILDREAD, CHILDWRITE);
PARENTWRITE->autoflush(1);
CHILDWRITE->autoflush(1);
if ($child = fork) # Parent code
{
close CHILDTREAD; # We don't need these in the parent
close PARENTWRITE;
while (<PARENTREAD>) {
print $_; }
close PARENTREAD;
close CHILDWRITE;
waitpid($child,0);
}else{
close PARENTREAD; # We don't need these in the child
close CHILDWRITE;
open(STDIN, ">&CHILDTREAD");
open(STDOUT, ">&PARENTWRITE");
open(STDERR, ">&PARENTWRITE");
exec('cat /etc/fstab');
}
Guardar a un fitxer
open OUTPUT, ">output.txt"; print OUTPUT "The quick brown fox\n"; close OUTPUT;
Fer les comprobacions inicials
die unless ($dir =~ /^[w-/._]+$/); die unless ($uid =~ /^d+$/);
Recorrer un array
@myNames = ('Larry', 'Curly', 'Moe');
foreach (@myNames) {
print $_;
}
Pujar fitxers per scp
#!/usr/bin/perl
use Net::SCP::Expect;
my $USER = "";
my $PASS = "";
my $HOST = "";
my $DIR = "/Comu/informes_pius/";
# List directory entries
opendir DIRH, $DIR;
@files = readdir(DIRH);
closedir DIRH;
# If there are files in the directory
if ($#files >= 2) {
my $scpe = Net::SCP::Expect->new;
$scpe->login($USER, $PASS);
$scpe->scp("$DIR/*","$HOST:.");
system("rm -f $DIR/*");
}
else {
print "No hi han fitxers a $DIR.\n";
}
