亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 6378 | 回復(fù): 5
打印 上一主題 下一主題

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2002-09-18 15:27 |只看該作者 |倒序?yàn)g覽
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&#59;
$y=&quot;I hate shell programming&quot;&#59;
$z=1.3e-27&#59;

這個(gè) $ 是表示現(xiàn)在操作的是個(gè)純量(相對(duì)於 list 或 associative array).
在  C 里的 operator, 如 +,-,*,/,%,^,++,--,+=,-=,*=,/=,%=,==,!=, &amp;&amp;,  
||, |, &amp; 全都有, 意思也不變. 不過若用於字串, . (dot)是表示連接的意思.
因此 .= 也有類似 C 中 += 的意思.

$x='I hate '&#59;
$x.=&quot;awk programming.&quot;&#59;
print $x&#59;

結(jié)果為 I hate awk programming. 字串可用單或雙括號(hào)圍住. 超出一行也沒關(guān).
也可以寫的像 shell programming 中:

$x= << END&#59;
This part can be put what
you like here
1234567890
till the sign as the beginning.
END
相當(dāng)於
$x=&quot;This part can be put what
you like here
1234567890
till the sign as the beginning.&quot;&#59;

字串的比較有 eq, ne, gt, lt, ....就像那 Fortran 的 operator. 聰明的
你猜猜就知道啦.

    至於 control 方面, 如 if, while, do .. while, 都和 C 類似. 如:

$sum=0&#59;
for($i=0&#59;$i<10&#59; $i++){
   $sum+=$i&#59;
}

大括號(hào)是不可省略的! 這和 C 不同. 也可:

$sum=0&#59;
$i=0&#59;
while($i<10){
   $sum+=$i++&#59;
}

如果你要從 loop 中跳出來, last 相當(dāng)於 C 中 break, next 相當(dāng)於 continue.

$sum=0&#59;
$i=0&#59;
while($i<10){
   if($i % 2==0){
      $sum+=$i++&#59;
      next&#59;
   }elsif($sum>;50){
      last&#59;
   }
}

   if 敘述要注意大括號(hào)是不可省, 還有 else if 必須寫成 elsif.

2.2 List
    List 和 1 dimension array 在 perl 中沒區(qū)別.  用法如:

@a=(1,2,3)&#59;
$a[0]-=$a[1]&#59;
print &quot;a[0]=$a[0]&quot;&#59;

結(jié)果就是 a[0]=-1. @是表示現(xiàn)在在操作 list. 雙括號(hào)的字串中,若有變數(shù), perl
會(huì)直接解譯. 如上例中的 $a[0]. List 可以直接連接:

@a=(1,2,3)&#59;
@b=(4,5)&#59;
@c=(@a,@b,6,7)&#59;
print &quot;@c&quot;&#59;

結(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)&#59;
push @a,(3,4)&#59;
結(jié)果 @a 成為 (1,2,3,4).

pop 拉最後一個(gè)元素 :
@a=(1,2,3)&#59;
$b=pop @a&#59;
結(jié)果 @a 成為 (1,2), $b 變成 3.

shift, unshift 和 pop, push 類似不過操作在 list 前端.
@a=(1,2,3)&#59;
$b=shift @a&#59;
unshift @a,0&#59;
結(jié)果 @a 成為 (0,2,3), $b 變成 1.

    Loop 也可寫成:

@weekday=( 'Mon','Tue','Wed','Thu','Fri','Sat')&#59;
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'&#59;
$phone_no{'john'}=&quot;06-1234567&quot;&#59;
$name='peter'&#59;
print &quot;$name Phone No.=&quot;,$phone_no{$name},&quot;\n&quot;&#59;

結(jié)果就會(huì)印一行:
peter Phone No.=02-9110238

   上面兩行也可寫成:
%phone_no=( 'peter' =>; '02-9110238', 'john' =>; '06-1234567')&#59;
=>; 和 , 一樣所以:
%phone_no=( 'peter' , '02-9110238', 'john' , '06-1234567')&#59;
也可, 就是相當(dāng)於一個(gè) key 和 value, 所組成的list.

2.4 Subroutine
    副程式可以任意擺放, perl 全看得到.

sub foo{
   my $x=$_[0]&#59;
   my $y=$_[1]&#59;
   return $x/$y&#59;
}
$x=1&#59;
$y=8&#59;
print foo(4,2)&#59;
結(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')&#59;
foreach (@weekday){
   # do with $_
}

結(jié)果 $_ 會(huì)依序變成  'Mon','Tue','Wed','Thu','Fri','Sat' 等.

   &#64&#59;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,&quot;test.dat&quot&#59;             # 寫入
open(FH,&quot;grep peter test.dat|&quot&#59;  # 先經(jīng) grep peter test.dat 處理後的 pipe 資料
open(FH,&quot;|more&quot&#59;                 # 後經(jīng) more 處理寫出的資料

   $oneLine=&#59;                 # 讀取 FH
   print FH &quot;1234&quot;&#59;               # 寫入 FH

   close(FH)&#59;                     # 關(guān)檔

例子:
open(FH,&quot;test.dat&quot&#59;              # 相當(dāng)於 open(FH, &quot{
   printf &quot;%4d:%s&quot;,++$n,$_&#59;
}
close(FH)&#59;
結(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=&quot;   end&#59; &quot;&#59;
if($line =~ /end/){
   print &quot;match!&quot;&#59;
}else{
   print &quot;NO!&quot;&#59;
}
結(jié)果會(huì)印出 match!, 如果改成
if($line =~ /^end/){
   print &quot;match!&quot;&#59;
}else{
   print &quot;NO!&quot;&#59;
}
結(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=&quot;Sep. 15th, Sep. 17th&quot;&#59;
$a =~ s/Sep/9/&#59;
結(jié)果是 9. 15th, Sep. 17th, 加 g (global) option 如下:
$d=&quot;Sep. 15th, Sep. 17th&quot;&#59;
$a =~ s/Sep/9/g&#59;
結(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&#59;
$phone{$name}[0]='1234'&#59;
$phone{$name}[1]='2674'&#59;

實(shí)際上 是用所謂的 reference,就像是 C 的 address. 一個(gè)純量可純存一個(gè)
reference.

@a=(1,2,3)&#59;
$aref=\@a&#59;          # 取 @a 的 reference
print $aref->;[0]&#59;   # 相當(dāng)於 print $a[0]&#59;
push @{$aref},4&#59;    # 相當(dāng)於 push @a,4&#59;

2-dimension array 可以如:
@a=([1,2,3],[4,5,6])&#59;
print $a[1][2]&#59;
結(jié)果是 6.

    使用 associative array 也類似:
$database{$name}{'home'}{'address'}='No. 1234, 75th Street&quot;&#59;
$database{$name}{'office'}{'phone'}='02-8127365&quot;&#59;

   List of associative array 也都可以使用.

2.9 Example
    Email 中若有 uuencode 的資料,可以寫 perl 將它 extract 出來.

#!/usr/local/bin/perl
open(EMAIL,&quot;<$ARGV[0]&quot||die &quot;Cannot open $ARGV[0]!&quot;&#59;
# 若開不成 perl 會(huì)跳出且印 Cannot open ...
while(){
   if(/^begin\s+\d\d\d\s+/){
      # 遇到 begin 開頭後印出
      print $_&#59;
      while(){
         if(/^end/){
            # 遇到 end 時(shí)結(jié)述
            last&#59;
         }
         print $_&#59;
      }
      print $_&#59;
      last&#59;
   }
}
close(EMAIL)&#59;
可以寫得更簡化,不過那就屬於您功力的無限空間了.........


-----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)制作 歡迎分享  ★

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2002-10-22 13:05 |只看該作者

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)

歡迎光臨。。。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2002-10-31 10:52 |只看該作者

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)

初學(xué)者值得一讀~

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2002-11-25 12:11 |只看該作者

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)

好!!

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2002-11-27 15:53 |只看該作者

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)

老大,如何用PERL操作informix online or informix ids????????

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2002-11-27 17:23 |只看該作者

[轉(zhuǎn)帖]Perl 簡介(適合對(duì) C 語言有點(diǎn)認(rèn)識(shí)的讀者)

xfeng7730 你在忙什么呀?
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP