siehe auch
lokale pfade zu perl-bibliotheken
perl -e 'print map "$_\n", @INC'
moduldokumentation in html umwandeln
pod2html --infile=/usr/share/perl5/XML/Writer?.pm --outfile=Writer.html
CPAN shell (zum laden und aktualisieren von perl modulen aus dem CPAN)
perl -MCPAN -e shell
ptkdb, ein grafischer perl-tk-debugger
perl -d:ptkdb scriptname.pl
my $anfang = (times)[0]; # benchmark (...) my $ende = (times)[0]; # benchmark printf "%.2f sec.\n", $ende - $anfang;
z.b. für ein logfile
use Fcntl ':flock';
open (ERRORLOG, ">>$error_log") || die "error_log kann nicht zum lesen geöffnet werden"; flock(ERRORLOG, LOCK_EX); print ERRORLOG "[$date] $error $file\n"; flock(ERRORLOG, LOCK_UN); close ERRORLOG;
nur dateien mit einer bestimmten dateiendung (hier *.ext)
my @ext_files = grep /ext$/, readdir(DIR);
nur unterverzeichnisse, aber nicht "." und ".."
opendir(DIR, $dir) my @subdirs = grep { !/^\.\.?$/ && -d "$dir/$_" } readdir DIR; close DIR;
siehe cgi.pm
"The Dump() method produces a string consisting of all the query's name/value pairs formatted nicely as a nested list"
print $query->Dump
aus TechDoc/AskDub1?
use CGI; my $cgi = new CGI; $add_url = $cgi->url(-query => 1); # vollst. URL plus parameter $add_url = $add_url."?" unless ( $add_url =~ /\?/g ); # (opt.) ? anfügen, für den 1. aufruf ohne parameter (...) $add_url = $add_url."¶m=value"; # neuer parameter anfügen
nützlich zum erneuen aufrufen des skripts, z.b. für eine funktion "formular felder hinzufügen" (to do)
siehe TechDoc/TicketLoginPl?
print $q->url(-path => 1); # gibt den string "included://localhost/draft/eingabe/" aus
die fileupload-funktion von cgi.pm ist im prinzip sehr komfortabel, allerdings auch sehr sensibel und fehleranfällig (wie auch immer mal wieder in einigen webforen nachzulesen ;)
beispiel: der aufruf eines perl-moduls, welches ebenso wie das script auf cgi.pm zugreift (auch wenn mit einem anderen konstruktor), bewirkt auf ersten blick keinen fehler, alle formular-parameter werden korrekt übergeben, nur der filehandle $fh = $q->upload('uploaded_file') ist leer.
workaround: da im modul nur die cookie-funktionen genutzt werden, relativ einfach, eine neue initialisierung (mit my $q = new CGI)entfällt.
modul bisher
use CGI; my $qq = new CGI; $value = $qq->cookie("name");
modul jetzt
use CGI::Cookie; my %cookies = fetch CGI::Cookie; $value = $cookies{name}{value}[0];
XML::LibXML (local: /usr/lib/perl5/XML/LibXML?)
aus /Ask23Collect
my $parser = XML::LibXML?->new(); my $tree = $parser->parse_file($xml_file); my $root = $tree->getDocumentElement?; my $owner = $root->findvalue('owner/@name'); my $info = $root->findvalue('info'); foreach my $node ($root->findnodes('ressource')) { my $ressource = $node->getAttribute('url'); push ( @ressourcen, $ressource ); }
$node->textContent
"this function returns the content of all text nodes in the descendants of the given node as specified in DOM."
xml
<element id="4" name="contributors" repeat="yes" type="optional" access="all"> <form tag="select" name="contributor_qualifier" iterations="3" accesskey="4b"> <option value="">option_description</option> </form> </element>
perl
foreach my $subnode ($node->findnodes('option')) { my $option_description = $subnode->textContent('option'); }
$node->textContent funktioniert nicht mit XML::LibXML?, version 1.31 (installiert unter debian woody mit perl 5.6.1. einführung von textContent ab v1.5, 20. mai 2002)
my $content = $node->textContent('content');
workaround:
my $content = $node->getFirstChild?->getData;
siehe http://www.linux-magazin.de/Artikel/ausgabe/2005/08/perl/perl.html
xml:
<index> <schlagwort name="Unbewusste, das"> <ressource url="/draft/archiv/md47_zeitschriften/riss.html.dat" /> </schlagwort> (....)
libxml mit xpath:
my $parser = XML::LibXML?->new(); my $tree = $parser->parse_file($xml_file); my $query = "Unbewusste, das"; for my $ressource($tree->findnodes('/index/schlagwort[@name="'.$query.'"]/ressource/@url')) { push ( @treffer, $ressource ); }
vgl. beispiele in http://userpage.fu-berlin.de/~corff/perl/perlkurs-6.html
vgl. http://mail.pm.org/pipermail/vienna-pm/2000-September/000695.html
@a = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, do { my $x = $_; $x =~ tr/äöü/aou/; $x =~ s/ß/ss/; $x; } ] } @a;
vgl. "programmieren mit perl: Hashes of Hashes" S.290ff 1 oder Perl Hash Howto
HoH? %element
use Data::Dumper; $Data::Dumper::Purity = 1; open (FILE, "> element.data") or die "can't open element.data: $!"; print FILE Data::Dumper->Dump([\%element], ['*element']); close FILE;
see also: 2
HoH? %element mit $element{name}{id}
# get hash of all id's my %id; for my $name ( keys %element ) { $id{$name} = ${element}{$name}{id}; }
# sort elements by id for my $name ( sort { $id{$a} <=> $id{$b} } keys %element ) { (...) }
oder http://www.devdaily.com/perl/edu/qanda/plqa00016/
Hash %element mit $element{name}
# sort all values,
sub hashValueDescendingNum? { # sort by value of the hash, descending $related_keywords{$b} <=> $related_keywords{$a}; } foreach my $keyword (sort hashValueDescendingNum? (keys(%related_keywords))) { print "$keyword => related_keywords{$keyword}\n"; }
siehe 3
e.g. sort by date_last_modified and then by title
my %hoh = %{$collection->{collections}}; foreach my $key (sort {"\L$hoh{$a}{date_last_modified}" cmp "\L$hoh{$b}{date_last_modified}" or "\L$hoh{$a}{title}" cmp "\L$hoh{$b}{title}" } keys %hoh) { print $hoh{$key}{title}." - ".$hoh{$key}{date_last_modified}; }
HoH? %index mit $index{status}{schlagwort}[url1, url2, ...]
wert hinzufügen
push @{ $index{$status}{$schlagwort} }, $ressource;
ausgabe
for my $status ( keys %index ) { print "status: $status\n"; for my $schlagwort ( keys %{ $index{$status} } ) { print "\t$schlagwort:\n"; for my $i ( 0 .. $#{ $index{$status}{$schlagwort} } ) { print "\t\t$index{$status}{$schlagwort}[$i]\n"; } } }
oder auch
for my $ressource( @{$index{$status}{$schlagwort}} ) { print "\t\t$ressource\n"; }
HoA? %element mit $element{name}[wert]
# make array of all values my @name = @{ $element{$name} };