1. 程式人生 > 其它 >perl語言十分鐘入門【零基礎可入】

perl語言十分鐘入門【零基礎可入】

零基礎,perl語言,10分鐘入門

1.Hello,World

#!/usr/bin/perl -w
print ("hello,world!n");
#print "hello,world!n";

說明: (1)第一行指定直譯器,-w引數表示提示警告(或者使用use strict命令,執行更嚴格的檢查); (2)第二行輸出hello, world!; (3)如果習慣c的函式方式,print的引數可以打括號; (4)第三行是註釋,註釋以#打頭; (5)如果習慣shell的方式,print的引數可以沒有括號; (6)雙引號內可以使用轉義字元;

不妨設檔名為helloworld.pm 程式的執行方法為: (1)perl helloworld.pm (2)chmod 755 helloworld.pm && ./helloworld.pm

2.常量 2.1數字 (1)Perl內部總按照“雙精度浮點數”儲存數字並執行運算; (2)0377=>八進位制;0xFF=>十六進位制; 2.2字串 (1)單引號表示字串,不轉義; (2)雙引號表示字串,轉義且解釋變數; 2.3字串操作符 (1)拼接操作符:“.”=>拼接字串; (2)重複操作符:“x”=>一個字串重複多次;

#!/usr/bin/perl -w
print ("hello,"."world!n");
print ("hello " x 3);

輸出結果是: hello,world! hello hello hello

最後要說明一點,Perl是弱型別語言,字串和數字會相互轉化,這一點和php一樣。

3.變數 (1)變數以$開頭,後接一個標示符; (2)如何用變數獲取使用者輸入? 使用,它獲取使用者的輸入(一般以換行結束),可以使用chomp去除結尾的換行符。

#!/usr/bin/perl -w
$count = 0;
while($count<10)
{
    chomp($input = );
    print($input);
    $count++;
}

(3)未定義變數 未定義的變數會賦予undef值,它既不是數字,也不是字串; 它有可能被當做數字0使用; 使用define函式可以知道一個變數是否被定義;

#!/usr/bin/perl -w
$var = undef;
print($var);
if(defined($var))
{
    print("defined!n");
}
else
{
    print("undefined!n");
}
$var++;
print($var);

它的輸出是: Use of uninitialized value in print at undef.pm line 3. undefined! 1

(4)變數的作用域 my和our可以指定變數的作用域 my指定為區域性作用域; our指定為全域性作用域(預設為our);

#!/usr/bin/perl -w
our $g_one = "global_onen";
$g_two = "global_twon";
 
{
    my $local_one = "local_onen";
    print($g_one);
    print($g_two);
    print($local_one);
}
print($g_one);
print($g_two);
print($local_one);

輸出為: global_one global_two local_one global_one global_two Use of uninitialized value in print at our_my.pm line 13.

4.陣列與列表 4.1陣列 和c的陣列使用非常類似: $array[0]=”a0″; $array[1]=”a1″; $array[2]=”a2″;

4.2列表 圓括號內的一系列值,構成列表: (1, 2, 3) (“hello”, 4) (“hello”, “world”, “yes”, “no”) qw(hello world yes no) (1..10) 說明: (1)第一行,列表元素為1,2,3; (2)第二行,列表元素為一個字串,一個數字; (3)第三行,列表元素為4個字串,好多引號和逗號啊; (4)第四行,wq操作符,用來建立字串列表,而不用輸入這麼多引號和逗號,效果同(3); (5)範圍操作符“..”,表示一個範圍,從左至右連續加一。

列表的賦值: ($v1, $v2, $v3) = qw(yes i am);

整個列表的引用,@操作符: @list = qw(yes i am); @none = (); @huge = (1..5); @stuff = (@list, @none, @huge);

pop和push操作符: (1)pop彈出列表末端元素; (2)push向列表末端壓入元素;

shift和unshift操作符: (1)shift移出列表首部元素; (2)unshift向列表首部壓入元素;

列表的輸出: (1)列表輸出,只輸出列表,元素間不含空格; (2)列表的字串化輸出,輸出列表,元素間加入空格; (3)foreach控制結果,可以依次取得列表中各個元素

#!/usr/bin/perl -w
@list = qw(yes i am);
@none = ();
@huge = (1..5);
@stuff = (@list, @none, @huge);
 
$pop_last = pop(@stuff);
print($pop_last);
push(@stuff, "hello");
 
$shift_first = shift(@stuff);
print($shift_first);
unshift(@stuff, "world");
 
print(@stuff);
print("@stuff");
 
$element=undef;
foreach $element (@stuff)
{
    print("$element!n");
}

輸出: 5 yes worldiam1234hello world i am 1 2 3 4 hello i! am! 1! 2! 3! 4! hello!

4.3預設變數$_ 該使用變數的地方,如果省略變數,則會使用預設變數$_。

#!/usr/bin/perl -w
$_="hello,world!";
print();

輸出是: hello,world!

5.函式 5.1函式定義與呼叫 (1)定義函式的關鍵字是sub; (2)函式呼叫的關鍵字是&; (3)可用return顯示返回,也可用一個數字隱式返回

#!/usr/bin/perl
$num=0;
sub sumAdd
{
    $num+=1;
    print("$numn");
    #return $num;   # 顯示返回
    $num;           # 隱式返回
}
 
&sumAdd;
&sumAdd;
print(&sumAdd);

執行結果為: 1 2 3 3

5.2函式的引數 (1)呼叫函式時可直接帶引數列表; (2)函式定義處使用“預設變數”獲取引數列表;

#!/usr/bin/perl -w
sub max
{
    return ($_[0]>$_[1]?$_[0]:$_[1]);
}
 
$big=20;
$small=10;
print(&max($big,$small));

輸出為: 20

6.程式輸入輸出 上文已經介紹過標準輸入,下面介紹其他幾種常見的輸入輸出。 6.1Unix工具輸入輸出:<> <>提供類似於Unix工具輸入輸出的功能,它提供的功能能夠很好的和cat/sed/awk/sort/grep等工具結合使用。

#!/usr/bin/perl -w
use strict;
while(<>)
{
    chomp();
    print("$_!!!n");
}

該指令碼的功能,是在輸入每行後面加上!!!,它幾處使用到了預設變數。 不妨設檔名為diamond.pm 不妨設hello.txt中有三行資料,分別是111,222,333 執行步驟: (1)chmod 755 diamond.pm (2)cat hello.txt | ./diamond.pm | cat 輸出結果: 111!!! 222!!! 333!!!

6.2格式化輸出:printf

#!/usr/bin/perl -w
$int_var = 2011;
$str_var = "hello,world";
printf("%dn%sn",$int_var,$str_var);

輸出結果為: 2011 hello,world

6.3檔案輸入輸出 Perl保留了6個檔案控制代碼:STDIN/STDOUT/STDERR/DATA/ARGV/ARGVOUT 上述6.1中的程式還能這麼執行: ./diamond.pm out.txt 則輸出結果會重定向到out.txt中

輸入輸出到檔案中中,需要開啟、使用、關閉檔案控制代碼 (1)開啟檔案控制代碼: open LOG, “>>log.txt”; open CONFIG, ” (2)關閉檔案控制代碼: close LOG; close CONFIG; (3)使用檔案控制代碼: print LOG (“hello,world!n”); print STDERR (“yes i am!n”); while() { chomp(); … }

也可以使用select關鍵字: print(“to stdout1!”); select LOG; print(“to log1″); print(“to log2″); select STDOUT; print(“to stdout2!”);

#!/usr/bin/perl -w
$input_file = "hello.txt";
$output_file = "out.txt";
 
open INPUT, "<$input_file"; open OUTPUT, ">>$output_file";
while(
<input type="text">)
{
    chomp();
    print OUTPUT ("$_!!!n");
}
close OUTPUT;
close INPUT;

說明:他的功能和之前的diamond.pm是一樣的。

7.雜湊hash 7.1雜湊的存取 $key=”am”; $hash_one{“yes”} = 3; $hash_one{“i”} = 1; $hash_one{$key} = 5; print($hash_one{“am”}); $value = $hash_one{“hello”}; # undef

7.2雜湊的引用 要引用整個雜湊,使用%操作符。 %hash_one = (“hello”,5,”world”,5); print ($hash_one{“hello”}); %hash_two = %hash_one;

7.3雜湊的鬆綁 雜湊可以轉化為鍵值列表,稱為雜湊的鬆綁,轉化後不保證鍵的順序,但值一定在鍵的後面。

#!/usr/bin/perl -w
%hash_one = ("hello",5,"world",5);
$hash_one{"yes"} = 3;
$hash_one{"i"} = 1;
$hash_one{"am"} = 2;
 
@array_one = %hash_one;
print ($hash_one{"hello"});
print ("@array_one");

輸出結果為: 5 yes 3 am 2 hello 5 world 5 i 1

7.4雜湊的反轉 建立值對應鍵的反轉雜湊。 %hash_reverse = reverse(%hash_one); 只有在鍵值一一對應的情況下才湊效,否則會有無法預期的覆蓋發生。

7.5雜湊的美觀賦值 雜湊的美觀賦值使用=>符號。 %hash_one = (“hello”,5,”world”,5,”yes”,3,”i”,1,”am”,2); 上面這種賦值方式很容易搞錯,特別是鍵值都是字串的時候。 %hash_one = ( “hello” => 5, “world” => 5, “yes” => 3, “i” => 1, “am” => 2, ); 美觀賦值,是不是看起來更美觀,更容易區分哈什的鍵值呢。

7.6雜湊的遍歷 (1)keys和values函式能返回所有鍵與值的列表,但列表內順序不保證。 @k = keys(%hash_one); @v = values(%hash_one);

(2)each函式能一一遍歷雜湊,返回鍵值對,非常適合於while等迴圈; while(($key, $value) = each(%hash_one)) { … }

示例程式碼:

#!/usr/bin/perl -w
%hash_one = (
    "hello" => 5,
    "world" => 5,
    "yes"   => 3,
    "i"     => 1,
    "am"    => 2,
);
 
@k = keys(%hash_one);
@v = values(%hash_one);
 
print("@kn");
print("@vn");
 
$key = undef;
$value = undef;
while(($key, $value) = each(%hash_one))
{
    print("$key=>$valuen");
}

輸出結果為: yes am hello world i 3 2 5 5 1 yes=>3 am=>2 hello=>5 world=>5 i=>1

7.7雜湊的查詢與刪除 (1)查詢一個鍵是否存在,使用exists函式; (2)刪除一個鍵,使用delete函式;

#!/usr/bin/perl -w
%hash_one=(
    "yes"  => 3,
    "i"    => 1,
    "am"   => 2,
);
 
delete($hash_one{"yes"});
if(exists($hash_one{"yes"}))
{
    print($hash_one{"yes"});
}

結果什麼也不輸出。

8.流程控制*(本節可跳過,都是些花裡胡哨的東西) 除了各語言常用的if/esle,for,while等流程控制外,Perl還有一些特有的控制語句,更人性化。 (1)unless控制結構 作用效果類似於if not,無效率上提升,只是使表達更自然,程式碼更容易理解。 (2)until控制結構 作用效果類似於while not (3)條件修飾 判斷條件可以直接寫在語句的後面,以增加可讀性(habadog注:這是鬼扯)。 print (“$n”) if $n < 0; $i *= 2 until $i > 1024; &sumAdd($_) foreach @num_list; (4)裸控制結構 只有一個花括號的結構,往往用來限制作用域,在各語言中都很常見。 { $a = 1; … } # $a失效了 (5)last控制結構 相當於c中的break,立刻終止迴圈; (6)next控制結構 相當於c中的continue,立刻開始下一次迴圈; (7)redo控制結構 …獨有的,重新開始本次迴圈; while(1) { # 跳到這裡 print (“hello”); redo; }

9.高階特性 神奇的Perl還有正則、module、檔案、字串、智慧匹配、程序管理、執行緒支援等高階特性,就不在入門手冊裡介紹了。 如果大夥喜歡,後續釋出以上特性的手冊。 希望你喜歡上Perl。