PERL Vortrag @ Computer Stammtisch Harburg 2008-07-30 ======================================================= Perl (Practical Extraction and Report Language), aka "Swiss Army chainsaw", is a high-level, general-purpose, interpreted, dynamic programming language. (wikipedia) - 1987 entwickelt von Larry Wall (NASA) als UNIX scripting language - Ideen von C, shell scripting, AWK, sed und Lisp aufgenommen - core interpreter, written in C (POSIX), together with a large collection of modules, written in Perl and C - MS Windows: ActivePerl (ActiveState), http://win32.perl.org/ - Artistic License and the GNU General Public License - perl is "The Duct Tape of the Internet" - TIMTOWTDI = "There's more than one way to do it." - grafisch: Perl/Tk, Perl/Qt, WxPerl, ... - "glue language/data munging" -- verschiedene Datenstrukturen, Systeme verbinden - graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web, LAMP, mod_perl - große Nutzer: bbc.co.uk, Amazon.com, LiveJournal.com, IMDb.com #!/usr/bin/perl use warnings; # um mehr Warnungen zu erhalten use strict; # compiler directives; strict: strenger, z.B. nur vorher definierte Variablen dürfen benutzt werden print "Hello World!"; Kommandozeile: perl -e 'while(<>){print}' perl script.pl Hilfe ----- perldoc perlfunc perldoc perlop perldoc IO::File (pod Dokumentation in Modulen) Datentypen ---------- - garbage collection - keine definierte Länge von Variablen! (selber überprüfen, tainted mode bei cgi Programmen) o Scalar "$" Zahlen, Strings, Referenz/Pointer, Bool my $scalar=""; (undef($scalar)) mögliche false Werte: undef, 0, 0.0, 0x0, 0b0, '0', "" $gesamt="string1".$string2; $viele="23"x23; o Array/Liste "@" Liste von Scalar my @liste=("wert1", "wert2"); Listenelement: $liste[0] anon. Liste: $p_liste=["wert1", "wert2"] Anzahl der Elemente: scalar(@liste) $#liste == scalar(@liste)-1 my $gesamtliste=(@liste1, @liste2); o Assoziatives Array/Hash "%" Liste von Scalar-Tupel (Key/Value) %hash=("key1"=>"value1", "key2"=>"value2"); my %hash=(); Hashelement: $hash{"key"}=value; anon. Hash: $p_hash={"key1"=>"value1"}; Liste aller Schlüssel: @liste=keys(%hash) Liste aller Werte: @liste=values(%hash) o Filehandle/Pipe o Konstanten (optional "&") o Funktion/Subroutine (optional "&") - shift, unshift, pop, push, ref, defined - Verkettung $liste1[9]->{"key"}->[23]=42; kurz: $liste1[9]{key}[23]=42; my %unique=(); foreach($wert...){ $unique{$wert}++; } Dereferenzieren --------------- %hash=%{$liste1[9]}; @liste2=@{$liste1[9]{"key"}}; $wert=$$pointer2scalar; Diamondoperator <> ------------------ <> ist geöffnete auf Kommandozeilen angegebene Datei oder Pipe while($zeile=<>){ chomp($zeile); print $zeile."\n"; } @alles=<>; $_, @_ ------ print $_ while($_=<>); @_ => Funktionen Backticks --------- ``, exec, system $ping=`ping -t 3 www.google.de`; @pingliste=split("\n", $ping); print join("\n", split(" ", `ping -t 3 www.google.de 2>&1`)); if, unless, ?: -------------- print "\n" if(); if(){ } elsif(){ } else { } print unless(); print "Hallo ".($gender eq "w")?"Frau":"Herr"; String: eq (equal), ne Zahlen: ==, !=, >=, <= CPAN ---- http://search.cpan.org starten: perl -e shell -MCPAN cpan> install Expect Funktionen ---------- sub funktion{ my($var, $p_array, $_hash)=@_; my @locallist=@{$p_array}; return($var); } anonyme Funktionen: %menu=( "start"=>sub(print "Start!"), "login"=>sub(print "Login!")); &{$menu{"login"}}; Funktionale Programmierung -------------------------- @liste=(1..10); @einmalfuenf=map($_*5, @liste); %einmalfuenf=map{$_ => $_*5} (split("\n", $input))[0..4]; my %unique=(); foreach my $wert (("123", "adw", "123", "asd", "213", "123")){ $unique{$wert}++; } print join("\n", (map($_."\t".$unique{$_}, sort{$unique{$a} <=> $unique{$b}}(keys(%unique))))[0..2] ); Regexp ------ mehr im Vortrag 2008-10-29! print $wert if($wert=~/^\d{1,3}$/); if($wert=~/^(\d+)asdkjhasd/){ print $1; } tainted mode "-T" ----------------- überprüft Eingabe-Variablen, ob "verseucht", sinnvoll bei CGI Skripten Wenn alle Eingabe-Variablen per regexp o.ä. überprüft worden sind, keine Fehlermeldung Module ------ IO::File Math::BigInteger Expect Tie::Hash Getopt -- 2008-07-30, Sven Uebelacker , Creative Commons BY-NC-SA 3.0 http://creativecommons.org/licenses/by-nc-sa/3.0/de/ https://www.uebelhacker.de/stammtisch/