- 論壇徽章:
- 0
|
Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)
http://www.fanqiang.com (2001-06-07 20:10:00)
##########################################################################
# 版權(quán)聲明 #
# #
# 本篇文章的版權(quán)為作者PinHong Chen先生所有. 允許網(wǎng)路上的非營利 #
# 轉(zhuǎn)載. 作者的電子郵件地址:honger.bbs@aidebbs.edu.tw #
# #
# 本文可在下列URL取得: #
# http://www.math.ncu.edu.tw/~chenym/perl/intro.html #
##########################################################################
From news.ncu.edu.tw!news.cc.nctu.edu.tw!news.csie.nctu.edu.tw!aide_board Wed Feb 14 23:57:10 1996
Path: news.ncu.edu.tw!news.cc.nctu.edu.tw!news.csie.nctu.edu.tw!aide_board
From: honger.bbs@aidebbs.edu.tw (Her Done Mine Chao!)
Newsgroups: tw.bbs.comp.www
Subject: Basic Introduction to Perl
Date: 13 Feb 1996 06:54:46 GMT
Organization: 灣學(xué)術(shù)網(wǎng)路 BBS 實(shí)驗(yàn)站
Message-ID: <3BCREM$5jj@aidebbs.edu.tw>;
X-Filename: www/M.824194486.A
NNTP-Posting-Host: eo4.ee.ntu.edu.tw
Lines: 307
這是小弟的一篇介紹 perl 的文章,希望對(duì)你有用:
Honger
PPPP EEEEE RRRR L
P P E R R L
PPPP EEEE RRRR L
P E R R L
P EEEEE R R LLLLLL
1. Introduction
這不是 perl 的完整介紹,只是想將 perl 向 C programmer 作個(gè)介紹,也許能
有所幫助!
perl 是由 Larry Wall 先生所寫. 目前的版本 5.002, 加入多項(xiàng)的功能. 請(qǐng)
看 perl.1, 那有詳細(xì)的條列.
一般而言, 由 awk, sed, shell programming 寫的東西可以輕易用 perl 來
處理,而且速度更快. 在 C program 中,特別是有關(guān)抽取文字檔中的資訊,加以轉(zhuǎn)化
處理,用 perl 寫會(huì)更加方便, 而且不用處處擔(dān)心 pointer 和 memory allocation
的問題. 當(dāng)然了, perl 執(zhí)行時(shí)會(huì)有一道轉(zhuǎn)成內(nèi)部表示式的關(guān), 真正 optimized
過的 C code 仍會(huì)比 perl code 快. 一般而言, perl code 會(huì)比 C code 短很多.
perl 在 regular expression, list(array) 和 associative array 有很方
便的語法和不錯(cuò)的執(zhí)行效率. 連 yacc (Berkeley yacc) 也可有產(chǎn)生 perl code 的
選項(xiàng), 這對(duì)於 prototyping 十分方便.
2. Perl Basics
2.1 Basic data structure and Control construct
perl 的 comment 是從 # 之後開始.而 perl 并不區(qū)分 string
, integer 和 float point number 的 data type, 統(tǒng)統(tǒng)以純量代表.例如:
# This is a comment line till the end of this line
$x=1.5;
$y="I hate shell programming";
$z=1.3e-27;
這個(gè) $ 是表示現(xiàn)在操作的是個(gè)純量(相對(duì)於 list 或 associative array).
在 C 里的 operator, 如 +,-,*,/,%,^,++,--,+=,-=,*=,/=,%=,==,!=, &&,
||, |, & 全都有, 意思也不變. 不過若用於字串, . (dot)是表示連接的意思.
因此 .= 也有類似 C 中 += 的意思.
$x='I hate ';
$x.="awk programming.";
print $x;
結(jié)果為 I hate awk programming. 字串可用單或雙括號(hào)圍住. 超出一行也沒關(guān).
也可以寫的像 shell programming 中:
$x= << END;
This part can be put what
you like here
1234567890
till the sign as the beginning.
END
相當(dāng)於
$x="This part can be put what
you like here
1234567890
till the sign as the beginning.";
字串的比較有 eq, ne, gt, lt, ....就像那 Fortran 的 operator. 聰明的
你猜猜就知道啦.
至於 control 方面, 如 if, while, do .. while, 都和 C 類似. 如:
$sum=0;
for($i=0;$i<10; $i++){
$sum+=$i;
}
大括號(hào)是不可省略的! 這和 C 不同. 也可:
$sum=0;
$i=0;
while($i<10){
$sum+=$i++;
}
如果你要從 loop 中跳出來, last 相當(dāng)於 C 中 break, next 相當(dāng)於 continue.
$sum=0;
$i=0;
while($i<10){
if($i % 2==0){
$sum+=$i++;
next;
}elsif($sum>;50){
last;
}
}
if 敘述要注意大括號(hào)是不可省, 還有 else if 必須寫成 elsif.
2.2 List
List 和 1 dimension array 在 perl 中沒區(qū)別. 用法如:
@a=(1,2,3);
$a[0]-=$a[1];
print "a[0]=$a[0]";
結(jié)果就是 a[0]=-1. @是表示現(xiàn)在在操作 list. 雙括號(hào)的字串中,若有變數(shù), perl
會(huì)直接解譯. 如上例中的 $a[0]. List 可以直接連接:
@a=(1,2,3);
@b=(4,5);
@c=(@a,@b,6,7);
print "@c";
結(jié)果是 1 2 3 4 5 6 7. perl 有特殊的變數(shù), $#a 來表示 @a 的最後 index,
所以上例中, $c[$#c] 就是 7, 也可寫成 $c[-1]. 那麼 $c[-2] 就是 6 了.
List 還有些 operator, push, pop, shift, unshift, splice, 其中
push 推一些元素入 list:
@a=(1,2);
push @a,(3,4);
結(jié)果 @a 成為 (1,2,3,4).
pop 拉最後一個(gè)元素 :
@a=(1,2,3);
$b=pop @a;
結(jié)果 @a 成為 (1,2), $b 變成 3.
shift, unshift 和 pop, push 類似不過操作在 list 前端.
@a=(1,2,3);
$b=shift @a;
unshift @a,0;
結(jié)果 @a 成為 (0,2,3), $b 變成 1.
Loop 也可寫成:
@weekday=( 'Mon','Tue','Wed','Thu','Fri','Sat');
foreach $x(@weekday){
# schedule something
}
結(jié)果 $x 會(huì)依序變成 'Mon','Tue','Wed','Thu','Fri','Sat' 等.
2.3 Associative Array
這是使用 string 來做 index 的 array, 一般叫作 hash.
$phone_no{'peter'}='02-9110238';
$phone_no{'john'}="06-1234567";
$name='peter';
print "$name Phone No.=",$phone_no{$name},"\n";
結(jié)果就會(huì)印一行:
peter Phone No.=02-9110238
上面兩行也可寫成:
%phone_no=( 'peter' =>; '02-9110238', 'john' =>; '06-1234567');
=>; 和 , 一樣所以:
%phone_no=( 'peter' , '02-9110238', 'john' , '06-1234567');
也可, 就是相當(dāng)於一個(gè) key 和 value, 所組成的list.
2.4 Subroutine
副程式可以任意擺放, perl 全看得到.
sub foo{
my $x=$_[0];
my $y=$_[1];
return $x/$y;
}
$x=1;
$y=8;
print foo(4,2);
結(jié)果是 2.
上例中, my 這個(gè) keyword 表示, $x, $y 是在 foo 圍內(nèi)有效 (local variable
in subroutine lexical scope).而 $_[0], $_[1] 用於傳參數(shù). perl 的 subroutine
呼叫參數(shù)都是放在固定 @_ 的 list 中.
2.5 Special Variables
perl 常使用的 default variable 是 $_.
@weekday=( 'Mon','Tue','Wed','Thu','Fri','Sat');
foreach (@weekday){
# do with $_
}
結(jié)果 $_ 會(huì)依序變成 'Mon','Tue','Wed','Thu','Fri','Sat' 等.
@;ARGV 是 perl 執(zhí)行時(shí)的 command line argument,$ARGV[0] 相當(dāng)於 C 中的
argv[1]. $ARGV[1] 相當(dāng)於 C 中的 argv[2]. C 中的 argv[0] 放在 $0.
%ENV 是 perl 執(zhí)行時(shí)的 environment variable, $ENV{'HOME'} 就是 user
的 home path, $ENV{'PWD'} 就是 current path.
其它特殊變數(shù)請(qǐng)參考 perlvar.1.
2.6 File Handle
perl 中主要開檔的方法:
open(FH,"test.dat" ; # 寫入
open(FH,"grep peter test.dat|" ; # 先經(jīng) grep peter test.dat 處理後的 pipe 資料
open(FH,"|more" ; # 後經(jīng) more 處理寫出的資料
$oneLine=; # 讀取 FH
print FH "1234"; # 寫入 FH
close(FH); # 關(guān)檔
例子:
open(FH,"test.dat" ; # 相當(dāng)於 open(FH, " {
printf "%4d:%s",++$n,$_;
}
close(FH);
結(jié)果會(huì)把 test.dat 加 4 位行號(hào)印出.( 只寫 時(shí)相當(dāng)於省略 $_= ).
2.7 Regular Expression
perl 對(duì)於 regular expression 有重新改寫. 好用且功能強(qiáng), 是 sed 和 awk 的
superset. 要求執(zhí)行 regular expression 比對(duì)的 operator 為 ~= ( match),
!= ( not match). regular expression 用 /.../ 括住.
$line=" end; ";
if($line =~ /end/){
print "match!";
}else{
print "NO!";
}
結(jié)果會(huì)印出 match!, 如果改成
if($line =~ /^end/){
print "match!";
}else{
print "NO!";
}
結(jié)果會(huì)印出 NO!
一般而言,
^ 表string 的開端,
$ 表string 的結(jié)束,
[a-z] 表 a 到 z 中任一字元
pat1 | pat2 表 pat1 或 pat2 皆可
x* 表 x 重 0 次或以上
x+ 表 x 重 1 次或以上
\d 相當(dāng)於 [0123456789], \D 就是相反
\w 相當(dāng)於 [_a-zA-Z0-9], \W 就是相反
\s 表示 space, \S 表非 space character
若要取代,則:
$d="Sep. 15th, Sep. 17th";
$a =~ s/Sep/9/;
結(jié)果是 9. 15th, Sep. 17th, 加 g (global) option 如下:
$d="Sep. 15th, Sep. 17th";
$a =~ s/Sep/9/g;
結(jié)果是 9. 15th, 9. 17th. 另外 i (case-insensitive) option 可不管大小寫.
2.8 Reference
Perl 5.0 之後允許 list of list 也就可能有 2-Dimension Array.
例如:
$a[0][0]=1;
$phone{$name}[0]='1234';
$phone{$name}[1]='2674';
實(shí)際上 是用所謂的 reference,就像是 C 的 address. 一個(gè)純量可純存一個(gè)
reference.
@a=(1,2,3);
$aref=\@a; # 取 @a 的 reference
print $aref->;[0]; # 相當(dāng)於 print $a[0];
push @{$aref},4; # 相當(dāng)於 push @a,4;
2-dimension array 可以如:
@a=([1,2,3],[4,5,6]);
print $a[1][2];
結(jié)果是 6.
使用 associative array 也類似:
$database{$name}{'home'}{'address'}='No. 1234, 75th Street";
$database{$name}{'office'}{'phone'}='02-8127365";
List of associative array 也都可以使用.
2.9 Example
Email 中若有 uuencode 的資料,可以寫 perl 將它 extract 出來.
#!/usr/local/bin/perl
open(EMAIL,"<$ARGV[0]" ||die "Cannot open $ARGV[0] !";
# 若開不成 perl 會(huì)跳出且印 Cannot open ...
while(){
if(/^begin\s+\d\d\d\s+/){
# 遇到 begin 開頭後印出
print $_;
while(){
if(/^end/){
# 遇到 end 時(shí)結(jié)述
last;
}
print $_;
}
print $_;
last;
}
}
close(EMAIL);
可以寫得更簡化,不過那就屬於您功力的無限空間了.........
-----By Pinhong Chen--------------------------------------------------------
V1.0 Feb 11, 1996
(http://www.fanqiang.com) 進(jìn)入【UNIX論壇】
--------------------------------------------------------------------------------
相關(guān)文章
Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者) (2001-06-07 20:10:00)
Perl 簡介 (2001-06-07 19:00:01)
★ 樊強(qiáng)制作 歡迎分享 ★
|
|