perl腳本基礎總結

1.  單引號字符串中的\n不會被當作換行符處理。 如:'\'\\'  -->  '\  。mysql

2.  雙引號linux

                字符串聯    "Hello"."World"."\n"  -->  HelloWrold 換行;sql

                字符串重複操做   "fred"×3  --> "fredfredfred";shell

                大小寫       "\l","\L"  -->  小寫 ; "\u","\U" --> 大寫 ; "\E" 結束。數據庫

3.  字符串運算windows

              ==     eq       相等數組

              !=      ne       不等less

              <       lt         小於函數

              >       gt        大於fetch

             <=      le        小於或等於

             >=      ge       大於或等於

4.  用戶輸入      <STDIN>

5.  chomp函數:去掉文本的"\n"換行符。讀寫文件時,要去掉換行符。 如:chomp($text<STDIN>);

6.  單行註釋:#  ;   多行註釋以 "=pod"開始,"=cut" 結束。

7.  undef: 沒有值時,表現爲0或空字符串。

8.  defined函數: 爲undef時,返回false,其他返回true。

9.  x 輸出遍數。如:print "love" x 3 -->lovelovelove.

10.  特殊數組索引:"$#rocks" ,最後一個元素索引。

11.  qw簡寫,代替 "" 功能。 如:qw(freds bany wilad dino);

12.  @rocks=qw /break slasle labv/;  #給數組賦值。

      @copy=@qwarry; #將一個數組拷貝到另外一個數組裏。

13.  pop:末尾取出元素;push:將一個或一列元素加在末尾。

      如:pop(@array);  push(@array,@others);  #將@others添加到@array末尾。

14.  shift 和 unshift 對數組首操做。 shift:取出首元素。unshift:將一個或一列加在首部。

15.  當把email地址插入雙引號時,$email="fred\@bedrock.edu";  $email='fred@bedrock.edu';

16.  perl最經常使用的默認變量:$_  。 如:  foreach(1..10){

                                                   print "I can $_!\n";

                                                   }          

17.  foreach控制結構

        如:  foreach $rock(qw/break Slee lawer/){

               print "One rock is $rock . \n"; #打印數組

             }

18.  reverse 逆轉操做,將一串列表按相反的順序返回。

        如: @fred=6..10;

              @bareny=reverse(@fred); #@bareny="10,9,8,7,6".

19.  sort:將輸入的一串列表根據內部的字符順序進行排序。

         如:ASCII字符串     @rocks =qw/breaks sholer rubble gertdned/;

                                     @sorted=sort(@rocks);

20.  強制轉換成標量,context,可以使用 scalar。

       如:print "I have",scalar @rocks,"rocks!\n";輸出爲數字。

21.  <STDIN>在列表context中:  chomp(@line=<STDIN>);  #讀入全部行,不包括換行符。

        文件輸入完成時:linux/MACOS x,用 ctrl+d 結束;DOS/Windows, 用 ctrl+z 結束。

22.  調用子程序時,返回值爲最後一個被計算的表達式。 @_ 是子程序的一個私有變量。      可使用「my」建立子程序的私有變量,也能夠在if ,untile ,foreach 塊中使用。

      如: my($fred,$bread); #定義兩個私有變量。

       若是新的私有變量沒有被賦值的話,標量變量 會自動賦值爲 undef ;數組變量 賦值爲 空列表。

23.  使用strict Pragma 嚴格檢查某代碼;use strict 迫使採用更嚴格的檢測。

24. 調用子程序 & 可省。

       如: sub divsion{

                   $_[0]/$_[1];

             }

            my $quotient=divsion 355, 113;  #調用,同 my $quotient=&divsion(355,113);

25.  嚴重錯誤和 die 函數

       如: if(!open LOG,">>logfile"){

               die "Cannot create logfile:$_!";  #$_ 輸出系統錯誤信息

            }

        通常,0表示正常;非0表示失敗。常見的:1-->命令行中語法錯誤; 2--> 運行錯誤  ; 3--> 沒有找到匹配文件。

26.  @ARGV  含義是包含了程序從命令行獲得的全部參數。輸入。

27.   複製文件: system('cp -r ./filea.txt ./map'); #將文件.txt複製到map文件夾下

29.   讀文件:  open(FILE,"file.txt");  # FILE 爲文件句柄。

                    my $record;

                    while($record=<FILE>){

                         print ("file record is :$record\n");

                    }

                   close(FILE);

30.  寫文件:open(FILE1,">write.txt");

                  syswrite(FILE1,"I konw it ! \n");  #將 "I konw it!"寫入文件。

                 close(FILE1);

        注:> 覆蓋寫入;>> 追加寫入。

31.  建立文件夾:mkdir(hello,0777)||die "not create!\n";

      建立文件: unless(-e "name.txt"){

                       open(FILE2,">name.txt") or &out_err("not create!\n");

                     }

       刪除文件:unlink("name.txt");

       建立目錄(可多級):mkpath(directoy,1,0711);       刪除目錄:rmtree(directoy,1,1);

       刪除文件夾:rmdir("hello")||die "not delete";

32.  數據庫:windows下安裝DBD:->cmd 輸入 ppm install DBD::mysql

33.數據庫鏈接: use DBI;

                      my $dbh=DBI->connect("DBI:mysql:database=數據庫;host=localhost;port:3308","用戶名","密碼",{'RaiseError'=>1});

                      my $sth=$dbh->prepare("select * from asset");

                           $sth->execute();

                       while(my $ref=$sth->fetchrow_hashref()){

                             print "$ref->{'id'}\t".

                                     "$ref->{'name'}\n";

                       }

                       $sth->finish();

                       $dbh->disconnect();

34.哈希:my %last_name=(

                       "fred"=>"finished",

                       "dion"=>"undef",

                       "barney"=>"rubble"  

                     );

35.  keys 和values 函數

          my %hash=("a"=>1,"b"=>2,"c"=>3);

          my @k=keys %hash;   #獲得key值

          my @v=values %hash;  #獲得values值

       注: @k和@v中的"a","b","c" 的順序可能不一樣,可是@k和@v仍是一一對應的。如"b"=>2,可能在第三位。

36.  each函數:迭代hash中的每一個元素。

      如:while(($key,$values)=each %hash){

                print "$key=>$values\n";

        }

37.  exists 函數:  if(exists $books{$dino}){ };

      delete函數: delete $books{$dino}; #刪除

38.  大小寫轉換: $_="I saw Barney with Fred.";

                       大寫:S/(fred)|barney/\U$1/gi;  #BARNEY

                       小寫:S/(fred)|barney/\L$1/gi;  #barney

39.split操做:分割      @fields =split /separtor/,$string;

                               如: @fileds=split/:/,"abc:def:d:h";

     join粘合函數: my $result = join $glue,@pieces;

                         如:my $x=join ":",4,5,6,7,8;    # 4:5:6:7:8

40. 控制條件 unless 除非條件爲真,不然執行塊中代碼。

                 unless($fred=~/^[A-Z-]\W*$/i){

                      print "The $fred";

                  }

      until:與while相反,當條件爲假時,重複執行。

41. $n=++$m; #先將變量加1,再取值;

      $n=$m++;#先取值,在加1.

42.  last會馬上結束循環。 for,foreach,while,until,裸塊。

       next 跳到當前循環的最後面,next以後進入下一輪循環。

       redo 調到當前循環的頂端,不進行條件表達式判斷以及接着本次循環(可放到循環體後面)。

      標籤塊: LINE:while(<>){

                         foreach(split){

                                  last LINE if/__END__/;

                            }

                      }

43.  三元操做符:Express?if_true_exp : if_false_exp

44.  localtime函數:時間轉換

       如:my $timestemp=1180630098;

            my  $date=localtime $timestemp;

     格式:my($sec,$min,$hour,$day,$mon,$year,$wdy,$yday,$isdst)=localtime $timestemp;

      my $now=gmtime;    #獲得當前時間

45.   chdir 改變工做目錄。 如:chdir "/etc" or die "cannot chdir to /etc:$! ";

        globbing:shell將每個命令行中的任何的文件名模式轉換成它所匹配的文件名。

                    如: my @all_files=glob "*";

                          my @pm_files=glob "*.pm";

                          my  @all_files=<*>;

 46.   目錄句柄:打開:opendir     讀入:readdir        關閉:closedir     (用法與打開文件類似)

        重命名: rename "old","new";

       改變文件或目錄權限:chomd  。 如:chomd 0075,"fred","bamey";

       chown :改變一批文件的全部者及所在的組。

             如: defined(my $user=getpwnam "merlyn");  #getpwnam將名字轉化爲數字

                   defined(my $group=getgrnam "users");   #getgrnam將組名轉化爲數字

                   chown $user,$group,glob "/home/merlyn/*";   #返回文件個數

        readlink:符號鏈接指向的地方。    如: my $perl =readlink "/usr/local/bin/perl";

47.   使用索引尋找字符串:index

          $where =index($big,$small);  #從0開始編號

          rindex: 某個字符串最後出現的位置。

          substr: $part = substr($string,$inintial_position,$length);   #inintial_position:從0開始的編號的初始位置。length:字符串長度。string:父串

                      如: my $mineral=substr("Fred J.Flintstone",8,5);  #獲得"Flint"

                         替換: substr("hello ,world",0,5)="GoodBye";  #GoodBye,world

 48.  sprintf 格式化數據,返回值爲被請求字符串,而非打印。

                如:my $money=sprintf "%.2f",2.49997;  #2.5049.  eval 捕捉錯誤,放在$@中。

 49.  使用grep在列表獲得元素。

                  如:my @odd_number=grep{$_%2} 1..1000;

50. perl 目前遇到的安裝模塊:main::tail

相關文章
相關標籤/搜索