linux系統一般自帶perl,可以在指令行運作。
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 = <STDIN>);
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_one\n";
$g_two = "global_two\n";
{
my $local_one = "local_one\n";
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)第四行,qw操作符,用來建立字元串清單,而不用輸入這麼多引号和逗号,效果同(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("$num\n");
#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("%d\n%s\n",$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
$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;
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("@k\n");
print("@v\n");
$key = undef;
$value = undef;
while(($key, $value) = each(%hash_one))
{
print("$key=>$value\n");
}
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、檔案、字元串、智能比對、程序管理、線程支援等進階特性,就不在入門手冊裡介紹了。