html5-img
1 / 83

Perl - TMTOWTDI

Perl - TMTOWTDI. 宋政隆 Perl User. Outline. What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …. What is Perl?. P ractical E xtraction and R eport L anguage. Wrong Answer!!. 病態地 不拘一格的 廢物 製表者. P athologically E clectic R ubbish L ister.

sutton
Télécharger la présentation

Perl - TMTOWTDI

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. Perl - TMTOWTDI 宋政隆 Perl User

  2. Outline • What is Perl? • Why learn/use Perl? • How to get Perl? • Things about Perl …

  3. What is Perl?

  4. Practical Extraction and Report Language Wrong Answer!!

  5. 病態地 不拘一格的 廢物 製表者 Pathologically Eclectic Rubbish Lister See http://perldoc.perl.org/perl.html#BUGS Just name it Perl

  6. 福利汽車 • 不賣調表車、法拍車、權利車、流當車、AB車、改裝車,我們給消費者的保證: • 車是給您買回去開的,不是給您買回去修理的。

  7. Perl • 方便、好用、穩定、功能強大、移植性高,我們給使用者的保證: • There's more than one way to do it (TMTOWTDI)

  8. 對 Perl 的印象

  9. Picture from DanCentury 對 Perl 的印象

  10. Google ‘perl’ suggestion

  11. Perl code 很難懂?

  12. 為什麼要會Perl?

  13. Why should I learn/use Perl? • Perl is fun • Perl is useful for… • Text processing • Web programming • System administration • Game programming • Scientific research • bio-informatics, linguistics • Open Source

  14. How to ‘get’ perl?

  15. 工欲善其事必先利其器 50% 以上的人, 因為沒有好用的 GUI 編輯器而不用 Perl 60% 以上的人, 因為覺得 Perl 很難而不想用…

  16. How to ‘get’ Perl – Windows • Strawberry Perl • For Windows only • Just the same as Perl elsewhere • http://strawberryperl.com/ • Recommended!! • ActivePerl • For Windows, Linux, Mac OS X, Solaris, AIX and HP-UX • http://www.activestate.com/activeperl/

  17. Padre – a Perl IDE

  18. Perl briefs Data types Control flow Regular expressions

  19. Data Types • Scalars • Arrays • Hashes • References • File Handles • Objects

  20. Scalars • Numbers • Decimal floating point • (Can be made integer, octal, hexadecimal) • Strings • Can contain any character • Can be null: “” • Can be undef

  21. Perl $int = 1; $float = 0.2; $negative = -3; $string = “hello”; C/C++ inti = 1; floatj = 0.2; intk = -3; char*str = “hey”; Scalars

  22. Strings • Single-quoted • characters are as shown with only two exceptions. • single-quote in a single-quoted string requires \’ • backslash in a single-quoted string requires \\ • Double-quoted • it will interpolate– calculate variables or control sequences. • For example • $foo = “myfile”; • $datafile = “$foo.txt”; • will result in the variable $datafile holding the string “myfile.txt” • Another example • print ‘Howdy\n’; will print: • Howdy\n • print “Howdy\n”; will print • Howdy • (\n is a control sequence, standing for “new line”).

  23. Scalar operators • Math, just like C/C++ • *, /, %, **, etc. • Strings • x to repeat the thing on the left • “hi” x 5 gives “hihihihihi” • . concatenates strings • “Hello” . “ World!” • Perl knows to convert when mixing these two types: • “3”*7 gives 21 • “3”.7 gives “37”

  24. Comparing Scalars Comparison Numeric String • Equal == eq • Not equal != ne • Less than < lt • Greater than > gt • Less or equal <= le • Greater or equal >= ge 8 < 25 TRUE! “8” lt “25” FALSE!

  25. Variables • A sign, followed by a letter, followed by pretty much whatever. • Sign determines the type: • $foo is a scalar • @foo is a array hold a list of scalars • %foo is a hash • Variables default to global (they apply in all parts of your program). This can be problematic. • local $var will make the variable active only for the current “block” of code. • my $var does the same, and dies when out the current “block”

  26. Using Arrays • Elements are indexed, from 0. • my@animals = (“frog”, “bear”, “elephant”); • $animals[2]; # elephant • Note: element is a scalar, so $ rather than @ • Subsections are “slices”. • my@mammals = @animals[1,2]; • Lots of functions for • push, pop … • splitting a scalar string into an array • my$sentence = “元智大學 資訊工程系”; • my@words = split(“ “, $sentence); # @words contains (“元智大學”, “資訊工程系”);

  27. Control flow • Control structures (generally like C/C++) • if / then / elsif / else • while • do {} while • do {} until • for () / foreach() # loops over a list • switch (NOTE: After perl 5.10) • … • Errors / warnings • die“message” terminates program and output “message”. • warn“message” give you a warning and keeps going.

  28. Hashes • “Associative arrays” • A set of • values (any scalar), indexed by • keys (strings) • Example • my%info; • $info{ “name” } = “宋政隆”; • $info{ “age” } = 32; • $info{ “future” } = undef; • hashes 與 arrays 可以摻在一起用 • arrays of arrays, arrays of hashes, hashes of arrays …

  29. Matching string patterns using regular expressions • m/pattern/ will check the last stored variable ($_) for pattern. • $var =~ m/pattern/; will check $var for pattern. • If the pattern is in $var, then • $var =~ m/pattern/ is TRUE. • If you “group” part of the pattern and it is present, • $var =~ m/(pattern)/ is true, AND, now a variable names $1 contains the first match it found. • Group more pieces of the pattern and the matches are stored in $2, $3, etc. • This only grabs the *first* match. To grab all, say • my @matches = ($var =~ m/(pattern)/g); • This will store every match in the array @matches.

  30. Regular expression \([Ii]f \|and \)*\(<i>[AC]\+<\/i>.\)\(and\)\?

  31. What’s a “regular expression”? . any single character * zero or more of the previous + one or more of the previous ? zero or one of the previous {n} match exact n times {n,} match at least n times {n,m} match at least n but not more than m times [] character class ^ beginning of the line $ end of the line \b word boundary \d \D digit / non-digit \s \S space / non-space \w \W word character / non-word character | or – match this or that () grouping

  32. Examples • 資工|資訊工程 “資工”or “資訊工程” • \d{2,3}-\d\d\d-\d\d\d\d 有區碼電話號碼 • \d{4}-\d\d\d\d\d\d 手機號碼 • (?\d{2,3}-)?\d\d\d-\d\d\d\d 區碼可有可無 • \b[aeiou]\w+ 母音開頭的字 • [[:alpha:]]+ 任意字母 • \b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b Email • [-+]?[0-9]*\.?[0-9]+ floating point

  33. Try yourself!!! Rubular

  34. Perl by examples 小事? 交給 Perl 就搞定了! Get a webpage’s PageRank 奇摩字典

  35. If you’re using a script... Example: Google Page Rank

  36. Example: Google Page Rank • http://pagerankalert.com/ • Maybe I can just write a parser for this page…

  37. Google Page Rank via LWP::UserAgent use LWP::UserAgent; # Web User Agent Class my$url = shift; my$ua = LWP::UserAgent->new(); push @{ $ua->requests_redirectable }, 'POST'; my$r = $ua->post ('http://pagerankalert.com/search', Content => [ 'pagina[address]' => $url, ]); $r->decoded_content =~ m/pagerank(\d+)for.+/; print$1; % perl pr.pl www.cse.yzu.edu.tw 6

  38. There's More Than One Way To Do It. What if I forgot to set $ua->requests_redirectable ?

  39. Google Page Rank via WWW::Mechanize use WWW::Mechanize; # Handy web browsing in a Perl Object my$url = shift; my$mech = WWW::Mechanize->new(); $mech->get ('http://pagerankalert.com/search‘); $mech->submit_form ( fields => [ 'pagina[address]' => $url, ]); $mech->content =~ m/pagerank(\d+)for.+/; print$1; % perl mpr.pl www.cse.yzu.edu.tw 6

  40. There's More Than One Way To Do It. Hey! they’re too handy! Is there exist any simpler way?

  41. Comprehensive Perl Archive Network CPAN is Your best friend!!

  42. Google Page Rank via WWW::Google::PageRank use WWW::Google::PageRank; my$url = shift; my$gpr = WWW::Google::PageRank->new(); printscalar($gpr->get ($url)); % perl mpr.pl www.cse.yzu.edu.tw 6

  43. What is CPAN • Since 1995-10-26 • Collection of Perl software/class/documentation • DRY (Don’t Repeat Yourself) • 8000+ authors • ~18000 modules • Testers report for ALL module

  44. 哇~ CPAN這麼好用我現在就來用 …可是…

  45. 國內 Perl 常見問題

More Related