Perl 起源於 Unix,雖已移植至 Windows,但底層系統差異導致 CGI 開發時存在關鍵差異。本文聚焦兩大實務痛點:檔案測試運算子可用性差異與郵件發送機制差異,提供跨平台相容的解決方案。
Unix 下 Perl 提供 27 個檔案測試運算子 (-r, -w, -x, -o, -e, -z, -s, -f, -d, -l, -p, -S, -b, -c, -t, -u, -g, -k, -T, -B, -M, -A, -C 等)。Windows 因檔案系統與權限模型差異,**僅支援 4 個核心運算子**:
| 運算子 | 功能 | 回傳值 | 跨平台可用 |
|---|---|---|---|
-d | 測試是否為目錄 | 布林值 | ✅ |
-e | 測試檔案/目錄是否存在 | 布林值 | ✅ |
-s | 測試檔案大小 | 位元組數 (0 視為假) | ✅ |
-w | 測試是否可寫入 | 布林值 | ✅ |
#!/usr/bin/perl
use strict;
use warnings;
my $file = 'perl.exe'; # 或傳入參數
if (-e $file) { # 存在性檢查 (跨平台安全)
my $size = -s $file; # 取得大小 (跨平台安全)
print "File size is: $size bytes\n";
} else {
print "Can't find $file\n";
}
# 寫入前檢查可寫性
(-w 'SomeFile') || die "Cannot write to SomeFile: $!\n";
# 避免使用 -r, -x, -o, -f, -l 等 Windows 不支援的運算子
# 若需判斷一般檔案,可用:(-e $file && !-d $file)
下載 blat.exe,透過 system() 或反引號呼叫:
my $blat = 'C:\blat\blat.exe';
my $to = 'user@example.com';
my $subject = 'Test from Perl CGI';
my $body = "This is a test mail.\n";
my $server = 'smtp.example.com';
system("$blat - -to $to -subject \"$subject\" -server $server -body \"$body\"")
== 0 or die "BLAT 發送失敗: $?";
缺點:依賴第三方工具、部署麻煩、效能差 (每封郵件啟動進程)。
僅需核心模組 Socket、IO::Socket::INET (標準庫內建),不需安裝 CPAN 模組。
use strict;
use warnings;
use Socket;
use IO::Socket::INET;
sub smtp_mail {
my ($recipient, $from, $realname, $subject, $body) = @_;
my $smtp_server = 'smtp.example.com'; # 可用的 SMTP 伺服器
my $smtp_port = 25; # 或 587 (TLS), 465 (SSL)
my $sock = IO::Socket::INET->new(
PeerAddr => $smtp_server,
PeerPort => $smtp_port,
Proto => 'tcp',
Timeout => 30
) or die "無法連接 SMTP 伺服器 $smtp_server:$smtp_port: $!";
# SMTP 會話流程
my $response;
sub read_response {
$response = <$sock>;
die "SMTP 錯誤: $response" unless $response =~ /^[123]/;
}
read_response(); # 220 歡迎
print $sock "HELO localhost\r\n"; read_response();
print $sock "MAIL FROM:<$from>\r\n"; read_response();
print $sock "RCPT TO:<$recipient>\r\n"; read_response();
print $sock "DATA\r\n"; read_response(); # 354
# 郵件標頭
print $sock "From: $realname <$from>\r\n";
print $sock "To: $recipient\r\n";
print $sock "Subject: $subject\r\n";
print $sock "Content-Type: text/plain; charset=UTF-8\r\n";
print $sock "\r\n"; # 標頭結束空行
print $sock "$body\r\n";
print $sock ".\r\n"; # 郵件結束
read_response(); # 250 確認
print $sock "QUIT\r\n";
close($sock);
return 1;
}
# 使用範例
smtp_mail(
'user@example.com',
'noreply@mysite.com',
'My Perl App',
'測試郵件主旨',
"這是郵件內容\n包含中文測試。"
);
use Net::SMTP;
my $smtp = Net::SMTP->new('smtp.example.com', Timeout => 30) or die "SMTP 連線失敗";
$smtp->mail('noreply@mysite.com');
$smtp->to('user@example.com');
$smtp->data();
$smtp->datasend("From: My App \n");
$smtp->datasend("To: user\@example.com\n");
$smtp->datasend("Subject: 測試\n");
$smtp->datasend("Content-Type: text/plain; charset=UTF-8\n\n");
$smtp->datasend("郵件內容\n");
$smtp->dataend();
$smtp->quit;
推薦順序: Net::SMTP (內建、面向物件、錯誤處理完善) > 純 Socket (教學用) > BLAT (僅限極舊環境)。
\n (LF),Windows \r\n (CRLF)。輸出 HTTP 標頭必須用 \r\n,內容建議用 \n 讓瀏覽器自動處理File::Spec 模組 (catfile, catdir) 處理路徑,勿硬編碼 / 或 \$ENV{UC $key}#!/usr/bin/perl,Windows 依副檔名關聯忽略此行,保留無妨CGI.pm (核心模組) 處理參數解析、HTML 生成,確保跨平台一致行為Perl CGI 跨平台開發的核心心法:**僅依賴核心模組、避開平台專有特性、檔案操作用 File::Spec、郵件用 Net::SMTP、行尾用 HTTP 標準 CRLF**。遵循此原則,同一套 CGI 程式碼可在 Unix/Linux/Windows Apache/IIS 下零修改部署。