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 ;