Perl

Similar documents
PowerPoint プレゼンテーション

Learn_Perl 3-02.pdf

untitled

台灣經濟新報資料庫

I/O Files讀寫檔案:

untitled

untitled

第五章 鄉鎮圖書館閱讀推廣活動之分析

untitled

投影片 1

untitled

Fuzzy Highlight.ppt

untitled

移民資料

個人教室 / 網路硬碟

Progperl.PDF

untitled

untitled

untitled

untitled

依據教育部八十九年 月 日臺(八九)技(二)字第 號函

94年度學習障礙補救教學進階研習

untitled

國立陽明大學輻射防護計畫書

行政院國科會九十一年度專題研究

廉 樂 不 廉 倫 理 廉 倫 理 領 不 參 領 不 若 不 不 不 不 利 聯 行 李 聯 例 律

untitled

untitled

untitled

大陸黨報集團化發展之研究

untitled

投影片 1

第一場

untitled

untitled

1

untitled

untitled

untitled

untitled

untitled

untitled

勞工安全衛生組織管理及自動檢查辦法修正條文對照表(草案)

untitled

中華民國第45屆中小學科學展覽會

第三章 我國非營業特種基金制度及運作現況

untitled

untitled

untitled

untitled

untitled

untitled

untitled

untitled

untitled

untitled

5

untitled

2004/9/30

壹、

untitled

經濟部標準檢驗局台南分局

【愛在台灣之路蔓延時】分場大綱

吃寒天真的能減肥嗎

untitled

PowerPoint Presentation

untitled

untitled

地方公共服務績效比較評量之探討—標竿學習策略的觀點

untitled

untitled

untitled

untitled

untitled

untitled

台南縣全民學區數位學習課程進階班—PhotoImpact 10

untitled

1209 th New Territories East Group Scout Troop 讀 練 理論 參 立臨 立 療 行 理 量 理 料 留 度 練 理 理 理 冷 練 理 力 理 不 不

untitled

untitled

九十三年第三期檔案管理工作研習營學員建議事項答覆情形彙整表

untitled

untitled

WTO/TBT重要通知

例 度 讀 讀 不 不 來 念 來 了 讀 不 不 讀 不 讀行 利 了 說 更 了 讀

untitled

untitled

untitled

untitled

untitled

排灣族

untitled

untitled

了 立 連 立 量 領 來 例 蘭 便 不 數 不 論 更 更 更 力 更 參 例 來 例 見 量 度 量 量 參 論 量 行 量 量 瑩 理 來 錄 量 量 不 力 省 力 立 力 量 量 量 了 量 便 錄 錄 錄 料 說 省 6

untitled

拾壹、技術運用之蔬果預冷

九十四年度提升服務品質績效報告

龍華科技大學

微處理機實習期末專題

untitled

Transcription:

Perl 磊

Goal Introduction The first perl program Basical coding style Variable Data structure Control structure Regular expression Lab Reference Outline

The first perl program Just type this following string [cc@ncu]% perl -e "print Hello world!! " Now you also can type this string in a file #!/usr/bin/perl print "hello world!!"; Execute the file [cc@ncu] perl hello.pl The result is the same Hello world!!

Basical coding style # comment #!/usr/bin/perl Unix 行 ; my my 來 數 use warninig; use strict;

Variable scalar variable ( 量 數 ) 數 $a = 1 + 2; # Add 1 and 2 and store in $a $a = 3-4; # Subtract 4 from 3 and store in $a $a = 5 * 6; # Multiply 5 and 6 $a = 7 / 8; # Divide 7 by 8 to give 0.875 $a = 9 ** 10; # Nine to the power of 10 $a = 5 % 2; # Remainder of 5 divided by 2 ++$a; # Increment $a and then return it $a++; # Return $a and then increment it --$a; # Decrement $a and then return it $a--; # Return $a and then decrement it

variable 串 $string = "Hello World!!"; $a = $b. $c; # Concatenate $b and $c $a = $b x $c; # $b repeated $c times between $a = 1; $$ = 2; print $a + $b; # 3 print "$a + $b\n"; # 1 + 2 print '$a + $b\n'; # $a + $b\n

variable \a 鈴 \d 數 \D 數 \e (escape) \f \n 行 \s ( 行 ) \S \t (Tab) \w 了 a-z A-Z 數 \W

variable 數 數 量 來 數 不 $var O $var123 O $_var O $123 X $state O

Goal Introduction The first perl program Basical coding style Variable Data structure Control structure Regular expression Lab Reference Outline

Data structure # 列 my @array; $array[0] = 'first'; $array[1] = 'second'; $array[2] = 'third'; my @array = ("first","second","third"); my ($array[0], $array[1], $array[2]) = qw/first second third.../; print $#array # 列 數

Data structure push/pop LIFO (Last In First Out) print "pop push example\n"; my @array = ("first","second","third"); print " 列 :$#array\n"; print "@array\n"; push(@array,'fourth'); print " 列 :$#array\n"; print "@array\n"; my $var = pop (@array); print "$var\n"; print " 列 :$#array\n"; print "@array\n";

Data structure sort # my @array = qw/45 33 75 21 38 69 46/; @array = sort(@array); my @array2 = qw{8-1 3-2 -6 4-15 10 9}; @array2 = sort{ $a**2 <=> $b**2 }(@array2); # -1-2 3 4-6 8 9 10-15

Goal Introduction The first perl program Basical coding style Variable Data structure Control structure Regular expression Lab Reference Outline

control structure 0 串 串 "0" undef my $true = (1 < 2); print $true;

if my $num = 3; if ($num < 5) { } print " "; 行 print " " if ($num < 5); "<" ">" ">=" "<=" "==" "!=" 數 "eq" "lt" "gt" "le" "ge" "ne" 串 行

if elsifelse if ($date eq ' ') {... } elsif ($date eq ' ') {... } elsif ($date eq ' ') {...... } else { print " $date\n"; }

while while (1) { # 裡 print " "; } 行 print " " while (1);

for my $result = 1; for (my $num = 1; $num <= 10; $num = $num + 1) { $result *= $num; } for my $num (1...10) { # $num 1 10 print $num; } for (1...10) { print $_; } 更 print for (1...10);

foreach print foreach (@array);

Goal Introduction The first perl program Basical coding style Variable Data structure Control structure Sub routine Regular expression Lab Reference Outline

sub subroutine {...... } 數 參數 數

sub hello { print "hello\n"; } &hello; &hello; # hello # &subroutine

參數 sub hello { print "@_\n"; } &hello("hello"); # hello &hello("perl"); # perl @_ 來 參數 列

2 參數 sub sumroutine { my $sum; $sum = 0; for (@_){ $sum += $_; } print "$sum\n"; } &sumroutine(1,2,3,4,5); # 1+..+5 &sumroutine(1...10); # 1+..+10

sub sumroutine { my $sum; $sum = 0; for (@_){ $sum += $_; } return "$sum\n"; } print &sumroutine(1,2,3,4,5); # 1+..+5 print &sumroutine(1...10); # 1+..+10

sub routine { local $val = "in sub"; print $val; } my $val = "out sub"; &routine print $val; 參數

Goal Introduction The first perl program Basical coding style Variable Data structure Control structure Regular expression Lab Reference Outline

Regular expression =~ not only on Perl m/pattern/ == m{pattern} == m pattern $string =~ m/$patten/ $string =~ m{$patten} $string =~ m $patten $string =~ m!$patten! $string =~ /$patten/ my $answer = "monger"; while (!(my $patten = <STDIN>) =~ /$answer/) { # 行 monger 串 print "wrong\n"; # 裡 };

Regular expression 量 - 零 數 * my @array = qw{ggle google gle gogle gooaogle}; my $answer = "go*gle"; # 量 while (1) { # if ((my $patten = <STDIN>) =~ /$answer/) { # print "*match*\n"; *match* *match* *not match* *match* *not match* } else { print "*not match*\n"; } };

量 - 數 + my @array = qw{ggle google gle gogle gooaogle}; my $answer = "go+gle"; # 量 while (1) { # if ((my $patten = <STDIN>) =~ /$answer/) { # print "*match*\n"; *not match* *match* *not match* *match* *not match* } else { print "*not match*\n"; } };

量 - 數 {min,max} my @array = qw{ggle google gle gogle gooaogle}; my $answer = "go{2,4}gle"; # 量 while (1) { # if ((my $patten = <STDIN>) =~ /$answer/) { # print "*match*\n"; } else { print "*not match*\n"; } }; *not match* *match* *not match* *not match* *not match* Hint: {3,} 不 數 {,8} 不 數

^ $ 串 串

s/pattern/replace/ my $content = "I love NCU"; print $content if ($content =~ s/ncu/taiwan/); s/pattern/replace/g my $content = "I love Perl. I am a perl monger"; print $content if ($content =~ s/perl/perl/gi);

my $content = "I like perl. \n I am a perl programmer. \n"; if ($content =~ /(like.*programmer)/s) { print "$1\n"; } %perl get_regex.pl like perl. I am a perl programmer

CPAN Comprehensive Perl Archives Network CAPNPLUS enhanced CPAN command install i 料 u quit 離 CPAN

CPANPLUS [cc@ncu]% perl -MCPAN -e"install CPANPLUS" [cc@ncu]% rehash [cc@ncu]% cpanp CPAN Terminal>install Cwd CPAN Terminal>install CGI

--Cwd [cc@ncu]% vim cwd.pl #!/usr/bin/perl -w use strict; use Cwd; my $dir = cwd(); print "$dir\n"; [cc@ncu]% perl cwd.pl /usr/home/cc/perltest CPAN:http://www.cpan.org/

--CGI use CGI; my $q = CGI->new; print $q->header(-charset => 'utf-8', - type => 'text/html'); header

<input> <input type="text" name="user"> $q->param('name');

CGI 例 <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> <FORM ACTION="print.pl" METHOD="POST"> <INPUT TYPE="text" NAME="name"><BR/> <INPUT TYPE="text" NAME="address"><BR/> <INPUT TYPE="text" NAME="tel"><BR/> <INPUT TYPE="submit"> </FORM> </BODY> </HTML>

#!/usr/bin/perl -w use CGI; my $q = CGI->new; print "Content-type: text/html;charset=utf-8\n\n"; print "<HTML><body>"; print " ".$q->param('name')."<br>"; print " ".$q->param('address')."<br>"; print " ".$q->param('tel')."<br>"; print "</body></html>";

料 連 # DBI CPAN Terminal>install DBI CPAN Terminal>install DBD::mysql use DBI; use strict; my $dbh = DBI->connect( DBI:mysql: 料,, ); my $sth = $dbh->prepare($statement); $sth->execute; $sth->finish; $dbh->disconnect;

料 $statement="insert INTO addressbook (name,phone,address) VALUES(' ','03-4227151 ',' 里 2 路 300 ')"; $statement="select * from addressbook";

LAB 老 料 http://www.cwb.gov.tw/v5/observe/real/real_d ata_46697.htm http://api.wunderground.com/weatherstation/ WXCurrentObXML.asp?ID=MC3721 LWP (Library for WWW in Perl )

Reference http://neural.cs.nthu.edu.tw/jang/books/perl/get WebPage02.asp PerlCookBook http://wiki.scifi-fan.idv.tw/perlcookbook Regex Programming @Jserv http://blog.linux.org.tw/~jserv/archives/001476.h tml

Database tables CREATE TABLE `Taoyuan` (`pk` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `date` VARCHAR( 15 ) NOT NULL, `time` VARCHAR( 15 ) NOT NULL, `temperature` VARCHAR( 15 ) NOT NULL ) ENGINE = MYISAM ;