Click here to load reader

Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl

  • View
    223

  • Download
    2

Embed Size (px)

Text of Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl?...

  • Slide 1
  • Perl - TMTOWTDI Perl User
  • Slide 2
  • Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl
  • Slide 3
  • What is Perl?
  • Slide 4
  • Practical Extraction and Report Language Wrong Answer!!
  • Slide 5
  • Pathologically Eclectic Rubbish Lister See http://perldoc.perl.org/perl.html#BUGShttp://perldoc.perl.org/perl.html#BUGS Just name it Perl
  • Slide 6
  • AB
  • Slide 7
  • Perl There's more than one way to do it (TMTOWTDI)
  • Slide 8
  • Perl
  • Slide 9
  • Picture from DanCentury Picture DanCentury
  • Slide 10
  • Google perl suggestion
  • Slide 11
  • Perl code ?
  • Slide 12
  • Perl ?
  • Slide 13
  • Why should I learn/use Perl? Perl is funfun Perl is useful for Text processing Web programming System administration Game programming Scientific research bio-informatics, linguistics Open Source
  • Slide 14
  • How to get perl?
  • Slide 15
  • 50% , GUI Perl 60% , Perl
  • Slide 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/
  • Slide 17
  • Padre a Perl IDE
  • Slide 18
  • Perl briefs Data types Control flow Regular expressions
  • Slide 19
  • Data Types Scalars Arrays Hashes References File Handles Objects
  • Slide 20
  • Scalars Numbers Decimal floating point (Can be made integer, octal, hexadecimal) Strings Can contain any character Can be null: Can be undef
  • Slide 21
  • Scalars Perl $int = 1; $float = 0.2; $negative = -3; $string = hello; C/C++ int i = 1; float j = 0.2; int k = -3; char* str = hey;
  • Slide 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).
  • Slide 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
  • Slide 24
  • Comparing Scalars ComparisonNumericString Equal==eq Not equal!=ne Less thangt Less or equal=ge 8 < 25 TRUE! 8 lt 25 FALSE!
  • Slide 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
  • Slide 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 ( , );
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 30
  • Regular expression \([Ii]f \|and \)*\( [AC]\+.\)\(and\)\?
  • Slide 31
  • Whats 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
  • Slide 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._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b Email [-+]?[0-9]*\.?[0-9]+floating point
  • Slide 33
  • Try yourself!!! RubularRubular
  • Slide 34
  • Perl by examples ? Perl ! Get a webpage s PageRank
  • Slide 35
  • Example: Google Page Rank If youre using a script...
  • Slide 36
  • Example: Google Page Rank http://pagerankalert.com/ Maybe I can just write a parser for this page
  • Slide 37
  • Slide 38
  • Slide 39
  • 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
  • Slide 40
  • There's More Than One Way To Do It. What if I forgot to set $ua ->requests_redirectable ?
  • Slide 41
  • 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
  • Slide 42
  • There's More Than One Way To Do It. Hey! they re too handy! Is there exist any simpler way?
  • Slide 43
  • Comprehensive Perl Archive Network CPAN is Your best friend!!
  • Slide 44
  • Google Page Rank via WWW::Google::PageRank use WWW::Google::PageRank; my $url = shift; my $gpr = WWW::Google::PageRank->new(); print scalar($gpr->get ($url)); % perl mpr.pl www.cse.yzu.edu.tw 6
  • Slide 45
  • What is CPAN Since 1995-10-26 Collection of Perl software/class/documentation DRY (Dont Repeat Yourself) 8000+ authors ~18000 modules Testers report for ALL module
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • ~ CPAN
  • Slide 50
  • Perl
  • Slide 51
  • ~ CPAN ? CPAN CPANPLUS cpanminus
  • Slide 52
  • CPAN % cpan Sorry, we have to rerun the configuration dialog for CPAN.pm due to some missing parameters... , , ,
  • Slide 53
  • CPANPLUS CPAN, CPANPLUS, Modules ( ?) % cpanp CPANPLUS::Shell::Default -- CPAN exploration and module installation (v0.88) *** Please report bugs to. *** Using CPANPLUS::Backend v0.88. ReadLine support enabled. ,
  • Slide 54
  • There's More Than One Way To Do It.
  • Slide 55
  • cpanminus http://search.cpan.org/dist/App- cpanminus/ http://search.cpan.org/dist/App- cpanminus/ % wget http://xrl.us/cpanmhttp://xrl.us/cpanm % chmod +x cpanm root , % cpanm done no questions
  • Slide 56
  • cpanminus screen screenshot
  • Slide 57
  • There's More Than One Way To Do It. What if there s NO Useful Module?
  • Slide 58
  • From jiings flickrflickr
  • Slide 59
  • Slide 60
  • Original Source code
  • Slide 61
  • http://tw.dictionary.yahoo.com/
  • Slide 62
  • http://tw.dictionary.yahoo.com/
  • Slide 63
  • Slide 64