2020-05-27 18:42:46 +03:00
|
|
|
|
---
|
|
|
|
|
category: language
|
2024-10-21 00:46:35 +03:00
|
|
|
|
language: Perl
|
2020-05-27 18:42:46 +03:00
|
|
|
|
filename: learnperl-tw.pl
|
|
|
|
|
contributors:
|
|
|
|
|
- ["Korjavin Ivan", "http://github.com/korjavin"]
|
|
|
|
|
- ["Dan Book", "http://github.com/Grinnz"]
|
|
|
|
|
translators:
|
|
|
|
|
- ["Kang-min Liu", "https://gugod.org"]
|
2020-05-29 03:45:02 +03:00
|
|
|
|
- ["Shih-Kai Chiu", "https://twitter.com/zard1989"]
|
2020-05-27 18:42:46 +03:00
|
|
|
|
lang: zh-tw
|
|
|
|
|
---
|
|
|
|
|
|
2020-05-29 03:39:07 +03:00
|
|
|
|
Perl 5 是一款強大且功能豐富的程式語言,已經持續發展超過 25 年。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-06-03 13:45:15 +03:00
|
|
|
|
從大型主機到行動裝置,Perl 5 能在上百種平台執行,適合快速打造產品原型,也適合大
|
|
|
|
|
型專案開發。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
```perl
|
|
|
|
|
# 註解列皆以井字號為開頭
|
|
|
|
|
|
|
|
|
|
#### 嚴謹度
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
|
|
# 所有的 perl 程式檔案都應當包含此兩列程式碼。在如變數名稱有拼寫錯誤之時,
|
|
|
|
|
# strict 能使編譯過程失敗。而對於像是將未定義值接到字串中等等易犯之錯誤,
|
|
|
|
|
# warnings 則能提供適當的警告訊息。
|
|
|
|
|
|
|
|
|
|
#### Perl 變數與其型別
|
|
|
|
|
|
|
|
|
|
# 變數的開頭皆為一印記(sigil),是為一符號,用以標示其型別。
|
|
|
|
|
# 變數名稱唯有以字母或底線開頭,後接字母、數字、底線若干,方為有效。
|
|
|
|
|
|
|
|
|
|
### 在 Perl 語言中,主要的變數型別有三種:$純量、@陣列、%雜湊。
|
|
|
|
|
|
|
|
|
|
## 純量
|
|
|
|
|
# 一個純量變數,只能裝一個值:
|
|
|
|
|
my $animal = "camel";
|
|
|
|
|
my $answer = 42;
|
|
|
|
|
my $display = "You have $answer ${animal}s.\n";
|
|
|
|
|
|
|
|
|
|
# 純量值可為字串、整數、浮點數。Perl 會自動地在需要之時進行轉換。
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 以單引號括住的字串內容與其字面之值完全相同。而以雙引號括住的字串,
|
|
|
|
|
# 其中則能內插變數與像是這種表示換列字符 "\n" 的控制碼。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
## 陣列
|
|
|
|
|
# 一個陣列,可以裝下很多值:
|
|
|
|
|
my @animals = ("camel", "llama", "owl");
|
|
|
|
|
my @numbers = (23, 42, 69);
|
|
|
|
|
my @mixed = ("camel", 42, 1.23);
|
|
|
|
|
|
|
|
|
|
# 陣列元素的存取,需要角括號。前方的印記為 $ 符號,表示只取一個值。
|
|
|
|
|
my $second = $animals[1];
|
|
|
|
|
|
|
|
|
|
# 欲知陣列之大小,在純量語境之下使用陣列便可。例如,將陣列裝到一個純量變數中。
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 又或者是使用 "scalar" 算符。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my $num_animals = @animals;
|
|
|
|
|
print "Number of numbers: ", scalar(@numbers), "\n";
|
|
|
|
|
|
|
|
|
|
# 陣列也能夠被安插在雙引號字串之內。各內容元素間隔,預設是一個空白字符。
|
|
|
|
|
|
|
|
|
|
print "We have these numbers: @numbers\n";
|
|
|
|
|
|
|
|
|
|
# 雙引號字串中,若有像電子郵件地址的部分,會被視為是在內插某個陣列的內容物。
|
|
|
|
|
# 請稍加留意。
|
|
|
|
|
|
|
|
|
|
my @example = ('secret', 'array');
|
|
|
|
|
my $oops_email = "foo@example.com"; # 'foosecret array.com'
|
|
|
|
|
my $ok_email = 'foo@example.com';
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
## 雜湊
|
|
|
|
|
# 一個雜湊,能裝下許多對的鍵與值:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my %fruit_color = ("apple", "red", "banana", "yellow");
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 善用空白與 "=>" 算符,就能將其排得得好看一些:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my %fruit_color = (
|
|
|
|
|
apple => "red",
|
|
|
|
|
banana => "yellow",
|
|
|
|
|
);
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 雜湊元素的存取,需要大括號。前方的印記仍為 $ 符號,表示只取一個值。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
my $color = $fruit_color{apple};
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 以 "keys" 與 "values" 兩個函數,則可一次取得雜湊中的所有鍵、所有值。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
my @fruits = keys %fruit_color;
|
|
|
|
|
my @colors = values %fruit_color;
|
|
|
|
|
|
2020-05-28 03:33:55 +03:00
|
|
|
|
# 關於純量、陣列、雜湊,在 perldata 文件之中,有更完整的描述。
|
|
|
|
|
# (perldoc perldata)
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 參照
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-06-03 13:45:15 +03:00
|
|
|
|
# 以參照能組出結構更為複雜的資料型別。
|
|
|
|
|
# 像是在陣列中放入雜湊、或是在雜湊裡放入陣列的雜湊。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my $array_ref = \@array;
|
|
|
|
|
my $hash_ref = \%hash;
|
|
|
|
|
my @array_of_arrays = (\@array1, \@array2, \@array3);
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 匿名陣列與匿名雜湊也是參照
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my $fruits = ["apple", "banana"];
|
|
|
|
|
my $colors = {apple => "red", banana => "yellow"};
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 在參照之前補上適當的印記,是為解參照。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my @fruits_array = @$fruits;
|
|
|
|
|
my %colors_hash = %$colors;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 以箭頭算符,便可在解參照同時存取其中一值。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my $first = $array_ref->[0];
|
|
|
|
|
my $value = $hash_ref->{banana};
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 欲深入了解參照,詳見 perlreftut 與 perlref 兩份文件
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 條件結構與迴圈結構
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# Perl 語言中亦具備常見的條件結講與迴圈結構。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
if ($var) {
|
|
|
|
|
...
|
|
|
|
|
} elsif ($var eq 'bar') {
|
|
|
|
|
...
|
|
|
|
|
} else {
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unless (condition) {
|
|
|
|
|
...
|
|
|
|
|
}
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 這算是可讀性較好的 "if (!condition)"
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 倒裝句型算是某「很 Perl 的」寫法
|
2020-05-27 18:42:46 +03:00
|
|
|
|
print "Yow!" if $zippy;
|
|
|
|
|
print "We have no bananas" unless $bananas;
|
|
|
|
|
|
|
|
|
|
# while
|
|
|
|
|
while (condition) {
|
|
|
|
|
...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $max = 5;
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 以 for 迴圈,$i 為迭代變數
|
2020-05-27 18:42:46 +03:00
|
|
|
|
for my $i (0 .. $max) {
|
|
|
|
|
print "index is $i";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for my $element (@elements) {
|
|
|
|
|
print $element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
map {print} @elements;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 迭代變數為 $_
|
2020-05-27 18:42:46 +03:00
|
|
|
|
for (@elements) {
|
|
|
|
|
print;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 對雜湊進行迭代(for 與 foreach 完全相同)
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
foreach my $key (keys %hash) {
|
|
|
|
|
print $key, ': ', $hash{$key}, "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 又是「很 Perl 的」倒裝句法
|
2020-05-27 18:42:46 +03:00
|
|
|
|
print for @elements;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 對一雜湊參照之中迭代,逐一走過其鍵與值
|
2020-05-27 18:42:46 +03:00
|
|
|
|
print $hash_ref->{$_} for keys %$hash_ref;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 正規表示式
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# Perl 中,對正規表示式的支援既廣亦深,在 perlrequick、perlretut 等各處文件中
|
|
|
|
|
# 都有更加完整的文件。不過,簡而言之:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 簡易比對
|
|
|
|
|
if (/foo/) { ... } # 若 $_ 內含 "foo" 則為真
|
|
|
|
|
if ($x =~ /foo/) { ... } # 若 $x 內含 "foo" 則為真
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 簡易取代
|
|
|
|
|
$x =~ s/foo/bar/; # 將 $x 中第一個出現的 foo 換為 bar
|
|
|
|
|
$x =~ s/foo/bar/g; # 將 $x 中所有出現的 foo 換為 bar
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 檔案與輸出入
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 以 "open" 函式開檔後,便可自檔案輸入或對其輸出
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 讀檔:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
|
2020-05-28 18:58:53 +03:00
|
|
|
|
|
|
|
|
|
# 寫檔(若檔案已經存在,舊內容會被清空):
|
2020-05-27 18:42:46 +03:00
|
|
|
|
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
|
2020-05-28 18:58:53 +03:00
|
|
|
|
|
|
|
|
|
# 寫檔(若檔案已經存在,會寫到檔尾去):
|
2020-05-27 18:42:46 +03:00
|
|
|
|
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 使用 "<>" 算符,能對檔案代號進行讀取。在純量語境下,會自檔案代號讀一列內容。
|
|
|
|
|
# 而在串列語境下,對讀入整個檔案。每一列都會成為串列中一項元素。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
my $line = <$in>;
|
|
|
|
|
my @lines = <$in>;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 以 "print" 函式,則可對檔案代號進行輸出。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
print $out @lines;
|
|
|
|
|
print $log $msg, "\n";
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 函式之撰寫
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 撰寫函式很是容易:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
sub logger {
|
|
|
|
|
my $logmessage = shift;
|
|
|
|
|
|
|
|
|
|
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
|
|
|
|
|
|
|
|
|
|
print $logfile $logmessage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 之後,使用起來就與內建函式無異:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
logger("We have a logger subroutine!");
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 模組
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 所謂模組,就是一組 Perl 程式碼,由一些函式組成,並可讓其他 Perl 程式碼來利用。
|
2020-06-03 13:45:15 +03:00
|
|
|
|
# 為了讓 perl 能找至,通常模組之副檔名 .pm 。
|
2020-05-28 18:58:53 +03:00
|
|
|
|
|
2020-05-27 18:42:46 +03:00
|
|
|
|
package MyModule;
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
|
|
sub trim {
|
|
|
|
|
my $string = shift;
|
|
|
|
|
$string =~ s/^\s+//;
|
|
|
|
|
$string =~ s/\s+$//;
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 自他處利用:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
use MyModule;
|
|
|
|
|
MyModule::trim($string);
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# Exporter 模組能將函式出口,好讓它們能被這樣利用:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
use MyModule 'trim';
|
|
|
|
|
trim($string);
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 有許多 Perl 模組能從 CPAN (https://www.cpan.org) 下載下來,各式各樣的機能讓你
|
|
|
|
|
# 能免於重新發明輪子。不少高人氣模組,如 Exporter,則是與 Perl 一同釋出、散佈。
|
|
|
|
|
# 更多關於 Perl 模組的細節,詳見 perlmod 文件。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 物件
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# Perl 中的物件,只是個參照,但同時又知道自己屬於哪個類別(package),於是對自身
|
|
|
|
|
# 調用方法(函式)時方知去何處尋找函式本體。在建構子(通常是 "new")中,都是以
|
|
|
|
|
# "bless" 函式來標記參照與其類別。只不過,若你使用像 Moose 或 Moo 模組的話,這些
|
|
|
|
|
# 都不必自己來(總之請繼續往下讀)。
|
|
|
|
|
|
2020-05-27 18:42:46 +03:00
|
|
|
|
package MyCounter;
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
|
my $class = shift;
|
|
|
|
|
my $self = {count => 0};
|
|
|
|
|
return bless $self, $class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub count {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
return $self->{count};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub increment {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
$self->{count}++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# 以箭頭運算符,便可對某類別或某物件呼叫某方法:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
use MyCounter;
|
|
|
|
|
my $counter = MyCounter->new;
|
|
|
|
|
print $counter->count, "\n"; # 0
|
|
|
|
|
$counter->increment;
|
|
|
|
|
print $counter->count, "\n"; # 1
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
# CPAN 上的 Moose 與 Moo 模組能助你撰寫類別本體。它們提供了建構子,與簡單易懂的
|
|
|
|
|
# 語法能來宣告屬性。前述的類別改寫之後,如下:
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
package MyCounter;
|
2020-05-28 18:58:53 +03:00
|
|
|
|
use Moo; # 同時也啟用 strict 與 warnings
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
has 'count' => (is => 'rwp', default => 0, init_arg => undef);
|
|
|
|
|
|
|
|
|
|
sub increment {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
$self->_set_count($self->count + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
2020-06-03 13:45:15 +03:00
|
|
|
|
# 物件導向程式設計於 perlootut 文件中有詳盡的說明。
|
|
|
|
|
# 此外,perlobj 文件中更涵蓋了底層實做之細節。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
```
|
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 常見問答集
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-29 03:39:07 +03:00
|
|
|
|
perlfaq 是問與答,涵蓋許多常見問題和解法,常常對該用哪些 CPAN 模組有很好的建議。
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
2020-05-28 18:58:53 +03:00
|
|
|
|
#### 延伸閱讀
|
2020-05-27 18:42:46 +03:00
|
|
|
|
|
|
|
|
|
- [perl-tutorial](http://perl-tutorial.org/)
|
2020-05-28 18:58:53 +03:00
|
|
|
|
- [Learn Perl](https://www.perl.org/learn.html)
|
2020-05-27 18:42:46 +03:00
|
|
|
|
- [perldoc](http://perldoc.perl.org/)
|
2020-05-28 18:58:53 +03:00
|
|
|
|
- 內建函式 : `perldoc perlintro`
|