83
Perl - TMTOWTDI 政政 Perl User

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

Embed Size (px)

Citation preview

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

Perl - TMTOWTDI宋政隆

Perl User

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

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

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

What is Perl?

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

Practical Extraction and Report Language

Wrong Answer!!

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

Pathologically Eclectic Rubbish Lister

See http://perldoc.perl.org/perl.html#BUGS

Just name it

Perl

病態地 不拘一格的

廢物製表者

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

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

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

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

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

對 Perl 的印象

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

對 Perl 的印象 Picture from DanCentury

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

Google ‘perl’ suggestion

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

Perl code 很難懂 ?

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

為什麼要會

Perl?

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

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

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

How to ‘get’ perl?

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

工欲善其事必先利其器

50% 以上的人 , 因為沒有好用的 GUI 編輯器而不用 Perl

60% 以上的人 , 因為覺得 Perl 很難而不想用…

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

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/

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

Padre – a Perl IDE

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

Perl briefsData types

Control flowRegular expressions

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

Data Types Scalars Arrays Hashes References File Handles Objects

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

Scalars Numbers

Decimal floating point (Can be made integer, octal, hexadecimal)

Strings Can contain any character Can be null: “” Can be undef

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

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”;

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

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”).

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

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”

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

Comparing ScalarsComparison 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!

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

Variables A sign, followed by a letter, followed by pretty m

uch 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 cu

rrent “block” of code. my $var does the same, and dies when out the current “b

lock”

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

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 (“元智大學” , “資訊工程系” );

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

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.

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

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 …

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

Matching string patterns using regular expressions m/pattern/ will check the last stored variable ($_) for p

attern. $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.

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

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

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

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

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

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

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

Try yourself!!! Rubular

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

Perl by examples

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

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

Example: Google Page Rank

If you’re using a script...

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

Example: Google Page Rank http://pagerankalert.com/

Maybe I can just write a parser for this page…

Page 37: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …
Page 38: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …
Page 39: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …

Google Page Rank via LWP::UserAgentuse 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

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

There's More Than One Way To Do It.

What if I forgot to set $ua->requests_redirectable

?

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

Google Page Rank via WWW::Mechanizeuse 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

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

There's More Than One Way To Do It.

Hey! they’re too handy!Is there exist any

simpler way?

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

Comprehensive Perl Archive

NetworkCPAN

is Your best friend!!

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

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

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

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

Page 46: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …
Page 47: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …
Page 48: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …
Page 49: Perl - TMTOWTDI 宋政隆 Perl User. Outline What is Perl? Why learn/use Perl? How to get Perl? Things about Perl …

哇 ~ CPAN 這麼好用

我現在就來用…可是…

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

國內 Perl 常見問題

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

哇 ~ CPAN 這麼好用

我要怎麼用呢 ?CPAN

CPANPLUScpanminus

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

CPAN% cpan <Module>

Sorry, we have to rerun the configuration dialog for CPAN.pm due to some missing parameters...

… 問問題 , 回答問題 … 再問一堆很煩的東西 … 要是失敗了 , 又要重來 … 苦手 動不動就問你問題 , 不答還不行

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

CPANPLUS 為了取代繁瑣指令的 CPAN, 結果… 要安裝 CPANPLUS, 要先安裝好多好多的 Modules ( 雞生蛋 蛋生雞 ?) 結果…

% cpanp <Module> CPANPLUS::Shell::Default -- CPAN exploration and module installation (v0.88) *** Please report bugs to <[email protected]>. *** Using CPANPLUS::Backend v0.88. ReadLine support enabled. … … 也問一堆很煩的東西 … 要是失敗了 , 會直接跳出 … 苦手

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

There's More Than One Way To Do It.怎麼連安裝程式也有

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

cpanminus http://search.cpan.org/dist/App-cpanminus/

安裝簡單% wget http://xrl.us/cpanm% chmod +x cpanm

沒有 root 權限 , 就幫你安裝在自家目錄% cpanm <Module>…done… no questions

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

cpanminus screen screenshot

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

There's More Than One Way To Do It.What if there’s

NO Useful Module?

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

奇摩字典文字介面 – 舊版 From jiing’s flickr

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

奇摩字典

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

Original Source code

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

奇摩字典 http://tw.dictionary.yahoo.com/

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

奇摩字典 http://tw.dictionary.yahoo.com/

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

New ydict.pl!

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

There's More Than One Way To Do It.

Do I really need to use regex to parse the result? What if the layout changed?

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

奇摩字典 - TMTOWTDI

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

YAML – YAML Ain't Markup Language# http://tw.dictionary.yahoo.comauthor: Cheng-Lung Sunghandle: http://tw\.dictionary\.yahoo\.comxtitle: //div[@class="summary"]//h3defs: //div[@class="def clr nobr"]errormsg: //p[@class="ico"]caption: .//div[@class="caption"]explain: .//p[@class="interpret"]example: ../..//p[@class="example"]

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

use WWW::Scramble

如果又改版了我們也不用再動到程式

只要將YAML 檔

重新編寫即可

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

Perl jobs?Indeed.com104.com

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

我學 Perl 有用嗎 ?

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

Indeed.com

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

Indeed.com

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

Indeed.com

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

學 Perl 有 $途 嗎 ?

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

學 Perl 有 $途 嗎 ?

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

學 Perl 有 $途 嗎 ?

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

學 Perl 有 $途 嗎 ?

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

學 Perl 有 $途 嗎 ? – 104.com 擅長工具 : 工作需求量 Perl : 63 Python : 39 Ruby : 9 PHP : 173 C++ : 641 Java : 541

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

有什麼跟 Perl 有關 但還沒講到的 ? Perl 5.10

Named captured regular expression? Perl 5.12

… Yada Yada operator Object-Oriented Programming

Moose Event-driven Programming

POE AnyEvent

Web Catalyst Jifty Plack

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

Perl books - Philippe Lin

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

Perl resources Web

http://perl.org/ http://use.perl.org/ http://perldoc.perl.org/ Perl Monks Perl Mongers

http://taipei.pm.org http://chupei.pm.org

CPAN IRC

Freenode: #perl.tw, #elixus, #chupei.pm irc.perl.org: #perl, #news, #perl-help

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

Q & A謝謝大家

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

Pictures, Comics from The truth about Perl Juan Paredes – Blog Script to see if Perl is working ! :E ! NO PERL xkcd.com PHP-Nuke: Management and Programming