====== Perl ====== //Perl// ist eine verbreitete Script-Sprache. ===== Hello World ===== #!/usr/bin/perl print "Hello World!\n"; [[https://github.com/akamola/helloworld/blob/master/helloworld.pl|"Hello World"-Beispiel bei GitHub]] ===== Typen ===== ^ Prefix ^ Typ ^ Beispiel ^ | ''$'' | Scalar | ''$string = 'Hello World';'' ''$number = 42;'' | | ''@'' | Array | ''@array = [1, 2, 3];'' | | ''%'' | Hash/Liste | ''%hash = {'one' => 1, 'two' => 3, 'three' => 3};'' | ==== Arbeiten mit Hashes ==== my %hash = ( 'one' => 1, 'two' => 2, 'three' => "Three" ); $hash{'two'} = 4; print $hash{'three'}; ==== Hashes Of Hashes ==== > In very large hashes, it may be slightly faster to retrieve both keys and values at the same time using each (which precludes sorting): > > while ( ($family, $roles) = each %HoH ) { print "$family: "; while ( ($role, $person) = each %$roles ) { print "$role=$person "; } print "\n"; } Quelle: [[http://docstore.mik.ua/orelly/perl3/prog/ch09_04.htm|Hashes of Hashes (Programming Perl)]] ===== Abfragen ===== if ($cond) { # Do something } else { # Do something else } if ($cond) { # Do something } elsif ($other_cond) { # Do something different } else { # Do something else } ==== Abfragen mit RegEx ==== if ($cond =~ /[0-9]{2}/) { # Cond is an number with two digits } ==== Switch ==== Wichtig: Um [[http://perldoc.perl.org/Switch.html|Switch]] benutzen zu können, muss das entsprechende Modul eingebunden werden! use Switch; switch ($foobar) { case "foo" { # Is equal to "foo" } case "bar" { # Is equal to "bar" } case "foobar" { # Is equal to "foobar" } else { # Default/Is something different } } ===== Operatoren ===== ==== Vergleichsoperatoren ==== ^ Deutsch ^ Englisch ^ Zahlen-Vergleich ^ String-Vergleich ^ | Gleich | Equal | ''=='' | ''eq'' | | Ungleich | Not Equal | ''!='' | ''ne'' | | Kleiner | Less than | ''<'' | ''lt'' | | Größer | Greater than | ''>'' | ''gt'' | | Kleiner-Gleich | Less than or equal | ''<='' | ''le'' | | Größer-Gleich | Greater than or equal | ''>='' | ''ge'' | ==== Spezielle Operatoren ==== Der Operator ''||='' ist eine Kombination aus dem logischen Oder-Operator ''||'' und dem Zuweisungsoperator ''=''. my $v = 0; $v ||= 'abc' eq 'abc'; print $v, " (expected 1)\n"; # If "abc" is equal to "abc" the output is "1" Quelle: [[http://www.misc-perl-info.com/perl-operators.html|Misc Perl Info: Perl Operators]] ===== Methoden ===== ==== Hello World ==== //Hello World//-Script mit der Standard-Methode ''main'', die beim Ausführen des Scripts automatich aufgerufen wird. #!/usr/bin/perl main: { print "Hello World\n"; } ===== Funktionen ===== * [[http://perldoc.perl.org/functions/grep.html|grep()]] * [[http://perldoc.perl.org/functions/length.html|length()]]: Länge eines Strings bestimmen * [[http://perldoc.perl.org/functions/mkdir.html|mkdir()]]: Ein Verzeichnis anlegen * [[http://perldoc.perl.org/functions/sort.html|sort()]]: Ein Array alphabetisch sortieren * [[http://perldoc.perl.org/functions/split.html|split()]]: Einen String anhand einer RegEx zerlegen * [[http://perldoc.perl.org/functions/substr.html|substr()]]: Einen String kürzen ===== Snippets ===== ==== Prüfen ob eine Variable undefiniert ist ==== Um zu überprüfen, ob eine Variable undefiniert ist (häufige Warnmeldung beim Ausführen eines Scripts: ''Use of uninitialized value in ...''), gibt es verschiedene Möglichkeiten: # Method 1 if (!defined($foo)) { # Do something ... } # Method 2 (example with string) $foo = ($bar) ? $bar : ''; ==== Länge eines Arrays bestimmen ==== my $count = scalar(@array); ==== String als Int parsen ==== $int = int("02"); ==== Prüfen ob eine Datei oder ein Verzeichnis existiert ==== if (-e "example.txt") { # Check if a file or directory exists } if (-d "cgi-bin") { # Check if a directory exists and/or if it is a directory } if (-f "example.txt") { # Check if a file exists and/or if it is a file } ==== Daten roh ausgeben ==== Um Daten roh auszugeben, z.B. um alle Eigenschaften eines Objekts einzusehen, kann der [[http://perldoc.perl.org/Data/Dumper.html|Data::Dumper]] verwendet werden: use Data::Dumper; print Dumper($foobar); ===== Links ===== * [[http://www.perl.org/|Perl]] * [[http://perldoc.perl.org/|Perl Programming Documentation]]