1 / 34

Perl P ractical E xtraction and R eport L anguage

Perl P ractical E xtraction and R eport L anguage. Nikita Shipilov , 2008. Keelest. D ünaamiline programeerimiskeel Protseduraalne, ning toetab nüüd ka objektorienteeritud programmeerimist Esimene release aastal 1987 Sai tuntuks oma jõulise regulaaravaldiste mootori pärast

oihane
Download Presentation

Perl P ractical E xtraction and R eport L anguage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PerlPractical Extraction and Report Language Nikita Shipilov, 2008

  2. Keelest Dünaamiline programeerimiskeel Protseduraalne, ning toetab nüüd ka objektorienteeritud programmeerimist Esimene release aastal 1987 Sai tuntuks oma jõulise regulaaravaldiste mootori pärast Kasutatakse tekstitöötluses

  3. Keelest Mõjutatud keeltest: AWK, BASIC, BASIC-PLUS, C, C++, Lisp, Pascal, sed, Unix shell Mõjutas: Python, PHP, Ruby, ECMAScript, Dao, Windows PowerShell OS: cross-platform Litsentsid: GNU General Public License, Artistic License

  4. Looja Larry Wall http://en.wikipedia.org/wiki/Larry_Wall

  5. Ajalugu 1987 – Perl 1.0 1988 – Perl 2 uuendatud regulaaravaldiste mootor 1989 – Perl 3on lisatud binaarandmete tugi 1991 – Programming Perlesimene de facto manuaal 1993 – Perl 4.036

  6. Ajalugu 1994 – Perl 5 uus interpretaator, on lisatud mõned uued komponendid (objektid, viidad, leksikaalsed muutujad, moodulid), Unicode tugi, vood, on laiendatud OOP tugi 1995 – (CPAN)Comprehensive Perl Archive Network on loodud ühine repositoorium Perl moodulitele (praegu umbes 11 000 skripti 5 000 autorilt)

  7. Ajalugu 2007 – Perl 5.10.0 switch-käsk"smart match operator" ~~ Perl 6 ???

  8. Tunnused Perl on esialgselt loodud tekstitöötluseks Täna aga keelt kasutatakse paljude laie-mate probleemide lahendamiseks (süstee-mide administreerimiseks, võrkude seadi-stamiseks, GUI, ...) Perl osutub olema rohkem praktiline kui ilus!

  9. Tunnused Üldine Perl’i struktuur on pärit C keelest (muutujad, avaldised, väärtustamine, koodi plokid, subroutines, ...), Listid – Lisp’ist, Hashes –AWK, Regulaaravaldised – sed-keelest.

  10. Hello, World #!/usr/bin/perl print "Hello, world!\n";

  11. Süntaks Perl’i programm koosneb deklaratsioonide järjendist, mis käivitatakse ülalt-alla. Tsüklid, alamrutiinid ja teised struktuurid võimaldavad hüpata erinevate koodi osade vahel. Perl’i süntaktilised elemendid on valitavad, mis tähendab, et programmeerida võib nii nagu tahad!

  12. Süntaks Ainuke asi, mida peab deklareerima, on muutuja: my $a;if ($a) {} Muutuja hoiab undef väärtust seni, kuni temale on määratud mingi väärtus (0 num-brite korral, “” String’ide korral)

  13. Süntaks Alamrutiinide deklareerimine käib järgmi-selt: sub myname;$me = myname $0 or die “…” Samuti võib laadida rutiinide deklaratsioo-nid require või use käskudega.

  14. Süntaks Komentaarid: # Iga käsk peab olema lõpetatud semikoo-loniga. Kui käsk on viimane plokis siis ‘;’ võib mitte panna. Kui plokk on defineeritud “sama koodi sees”, siis selle eraldamiseks on vaja kasu-tada sulge { } if (EXPR) BLOCKif (!open(FOO)) { die "Can't open $FOO: $!"; }

  15. Süntaks Number 0, String ‘’ ja tühi list on undef, ja vastavalt on väärad Boole’i kontekstis. Kõik teised väärtused on tõesed. NB! Sümbolid on case-sensitive!

  16. Andmetüübid Perl’is on sisseehitatud kolm andmetüüpi: skalaarid, skalaaride massiivid ja assotsi-atiivsed massiivid (hashes). Skalaarid on kas sõna, number või viit millegile. Massiivid on järestatud skalaaride listid, mis on indekseeritud numbritega (alates 0-st). Hashes on võtmetega indekseeritud skalaaride kollektsioonid.

  17. Andmetüübid Lisatakse veel failide “käsitlejad” (filehand-les) ja alamrutiinid (subroutines). $foo # a scalar @foo # an array %foo # a hash &foo # a subroutine. FOO # a file handle or constant

  18. Andmetüübid Näide: $name = “joe”; $number = 50; @scores = {1, 10, 25, 15}; %favorite = ( joe => 'red', sam => 'blue' ); # Defining a subroutine sub foo { ... } foo $x, @y, %z;

  19. Standart IO open(IN, "< input.txt") or die "cant open input file: $!"; open(OUT, ">> output.txt") or die "cant open output file: $!"; while ($line = <IN>) { # write data to a file print OUT $line; } close(IN); close(OUT); print "$filename exists" if (-e $filename); print "$filename file size is ".(stat $filename)[7]; @txtfiles = <*.txt>; # perl globbing @txtfiles = `dir /b *.txt`; # or use the shell (slower), needs chomping

  20. Struktuurid Peamised struktuurid, mis kasutatakse voo kontrollimiseks, on järgmised: if (EXPR) BLOCK if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK LABEL while (EXPR) BLOCK LABEL while (EXPR) BLOCK continue BLOCK LABEL until (EXPR) BLOCK LABEL until (EXPR) BLOCK continue BLOCK LABEL for (EXPR; EXPR; EXPR) BLOCK LABEL foreach VAR (LIST) BLOCK LABEL foreach VAR (LIST) BLOCK continue BLOCK LABEL BLOCK continue BLOCK

  21. Struktuurid Rohkem informatsiooni: http://search.cpan.org/dist/perl/pod/perlsyn.pod Ja operaatorite kohta: http://search.cpan.org/dist/perl/pod/perlop.pod

  22. Regulaaravaldised Regulaaravaldiste mootor Perl’is põhineb backtracking algoritmil (brute-force). Avaldiste süntaks oli esialgselt võetud Unix Version 8 süsteemist. Kuid praegu Perl’i regexp pakub palju rohkem võimalusi.

  23. Regulaaravaldised Võrdlusoperatsioonid saavad kasutada erinevaid laiendeid (modifiers): • m – uuri teksti kui mitmerealist sõnet, • s – üherealine sõne, • i – case-insensitive pattern matching • x – luba tühikuid ja kommentaare • p – hoia ${^PREMATCH}, {$^MATCH}ja${^POSTMATCH}edasiseks kasutamiseks • g ja c – globaalne võrdlus, kursor samal kohal

  24. Regulaaravaldised Metasümbolid: • \ Quote the next metacharacter • ^ Match the beginning of the line • . Match any character (except newline) • $ Match the end of the line (or before newline at the end) • | Alternation • () Grouping • [] Character class

  25. Regulaaravaldised Kvantorid: • * Match 0 or more times • + Match 1 or more times • ? Match 1 or 0 times • {n} Match exactly n times • {n,} Match at least n times • {n,m} Match at least n but not more than m times

  26. Regulaaravaldised • *? Match 0 or more times, not greedily • +? Match 1 or more times, not greedily • ?? Match 0 or 1 time, not greedily • {n}? Match exactly n times, not greedily • {n,}? Match at least n times, not greedily • {n,m}? Match at least n but not more than m times, not greedily

  27. Regulaaravaldised “Greedy" tähendab seda, et avaldisi otsitakse teksti lõpuni, ja tagastatakse pärast ainult etteantud matching’ute arv.

  28. Regulaaravaldised Possessiivkvantorid: • *+ Match 0 or more times and give nothing back • ++ Match 1 or more times and give nothing back • ?+ Match 0 or 1 time and give nothing back • {n}+ Match exactly n times and give nothing back (redundant) • {n,}+ Match at least n times and give nothing back • {n,m}+ Match at least n but not more than m times and give nothing back

  29. Regulaaravaldised Sümbolite klassid: http://search.cpan.org/dist/perl/pod/perlre.pod#___top

  30. Regulaaravaldised Näide: my $email = "this.is\@my-email.id"; if ($email =~ /(.+)@(.+)\.(.{2,4})/) { print "E-mail : $email\n"; print "ID : $1\n"; print "Domain : $2\n"; print "Tail : $3\n"; } E-mail : this.is@my-email.id ID : this.is Domain : my-email Tail : id

  31. Smart matching $b ~~ $a: $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= Code[+] Code[+] referential equality $a == $b Any Code[+] scalar sub truth $b->($a) Hash Hash hash keys identical [sort keys %$a]~~[sort keys %$b] Hash Array hash slice existence grep {exists $a->{$_}} @$b Hash Regex hash key grep grep /$b/, keys %$a Hash Any hash entry existence exists $a->{$b} Array Array arrays are identical[*] Array Regex array grep grep /$b/, @$a Array Num array contains number grep $_ == $b, @$a Array Any array contains string grep $_ eq $b, @$a

  32. Smart matching $b ~~ $a: $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= Any undef undefined !defined $a Any Regex pattern match $a =~ /$b/ Code() Code() results are equal $a->() eq $b->() Any Code() simple closure truth $b->() # ignoring $a Num numish[!] numeric equality $a == $b Any Str string equality $a eq $b Any Num numeric equality $a == $b Any Any string equality $a eq $b ! - either a real number, or a string that looks like a number

  33. Viited • http://www.perl.com • http://www.perl.org • http://en.wikipedia.org/wiki/Perl • http://www.google.com

  34. Tänan!

More Related