也許不是最好的建議方式,但至少提供一致性的處理方式。
編輯器設定:UTF-8編碼
Perl程式:
- use utf8;
- 使用 encode 明確以big5編碼與外部OS(Windows)溝通
- $file_name = '中文檔名';
- encode ('big5', $file_name)
- 使用 decode 明確指定由外部進來的資料編碼為何
- $cname = decode('big5', $cname);
故常無欲,以觀其妙。
每天一早便開車加入 上班族長長婉蜒的車隊
一樣的風景 一樣的擁擠 一樣的忙碌 一樣的寂寞
不停的工作 不停的計劃 經歷希望、欲望、健忘和淡忘
數著歲月 數著白髮 數著青春 年華的流逝
盼望著有一天我能離去 去跋涉遙遠的高原和大漠
朝著普陀 聽那海潮和梵音
走回家鄉 夢回童年 走回家鄉 夢回童年
從小到大立下許多志向 多少披星戴月的夜車路
多少考試 多少窄門 多少遙遠的異鄉路
愛情來又去 空留回憶 親朋難相聚 多少生別離
多少煩惱 多少牽掛 此心何時 才能歇息
幻想著有一天我已離去 去尋找恆何舊墟的歷史
在那雪山和荒野裡沉思
從哪裡來 往何處去 從哪裡來 往何處去
use strict;
use BerkeleyDB;
use MLDBM qw(BerkeleyDB::Btree);
use Data::Random qw(:all);
my $filename = 'foo.db';
my %db;
tie %db, 'MLDBM', -Filename => $filename,
-Flags => DB_CREATE
or die "Cannot open database $filename: $!\n";
# 產生10筆隨機資料,放入%db中,隨著迴圈的執行,資料也會同步寫到硬碟上的檔案中。
for my $seq (1..10) {
my @random_words = rand_words( size => 5 );
$db{$seq} = [@random_words];
}
use strict; use DateTime; # 取得現在的時間,沒有指定Time Zone,所以是UTC Time Zone。 my $dt = DateTime->now; # 指定Time Zone是local,DateTime模組會嘗試取得正確的Time Zone my $dt = DateTime->now( time_zone => 'local' ); # 指定Time Zone是'Asia/Taipei' my $dt = DateTime->now( time_zone => 'Asia/Taipei' ); # 底下兩行是一樣的 # 2011-08-26T17:12:31 print $dt->datetime(), "\n"; print $dt->iso8601, "\n";
use strict; use Time::Piece; # Time::Piece模組會取代原有的localtime & gmtime # localtime會傳回Time::Piece物件 my $dt = localtime; # Time::Piece物件在字串語境下,也會自動跟localtime的輸出是一樣的 print "Time is $dt\n"; # Time is Fri Aug 26 17:12:31 2011 print "Year is ", $dt->year, "\n"; # Year is 2011 print $dt->datetime, "\n"; # 2011-08-26T17:12:31
use Term::ShellUI;
my $term = new Term::ShellUI(
commands => {
"cd" => {
desc => "Change to directory DIR",
maxargs => 1,
args => sub { shift->complete_onlydirs(@_); },
proc => sub { chdir($_[0] || $ENV{HOME} || $ENV{LOGDIR}); },
},
"chdir" => { alias => 'cd' },
"lsl" => {
desc => "List files in working directory",
maxargs => 0,
proc => sub { system('ls -l'); },
},
"pwd" => {
desc => "Print the current working directory",
maxargs => 0,
proc => sub { system('pwd'); },
},
"help" => {
desc => "Print helpful information",
args => sub { shift->help_args(undef, @_); },
method => sub { shift->help_call(undef, @_); }
},
"exit" => { alias => 'quit' },
"quit" => {
desc => "結束作業", maxargs => 0,
method => sub { shift->exit_requested(1); },
}},
history_file => '~/.shellui-history',
);
print 'Using '.$term->{term}->ReadLine."\n";
$term->prompt('OP-Shell: ');
$term->run();
# Linux下,將目錄內所有 *.nrg轉成 *.iso檔
# 使用 nrg2iso 指令
use strict;
my @nrg = glob "*.nrg";
for my $nrg (@nrg) {
# 需要將副檔名 .iso 改成 .nrg
my $iso = $nrg;
$iso =~ s/\.nrg$/\.iso/;
# 印出 nrg2iso 的輸出
print `nrg2iso "$nrg" "$iso"`;
}
c:\> cpan cpan> look POE > perl Makefile.PL > dmake > dmake test > dmake install exit
dmake.EXE: makefile: line 1312: Error: -- Input line too long, increase MAXLINELENGTH
c:\> cpan cpan> look DateTime::TimeZone > perl Makefile.PL > dmake MAXLINELENGTH=30000 > dmake MAXLINELENGTH=30000 test > dmake MAXLINELENGTH=30000 install
install DBD::DB2就可以了
# demo 使用Open來與外部程式交談
# unix指令wc : print newline, word, and byte counts for each file
# 在perl程式內執行wc, 提供計算資料以及取回計算結果
use strict;
use IPC::Open2;
use IO::Handle;
# init file handle
my $read_fh = IO::Handle->new();
my $write_fh = IO::Handle->new();
# 使用open2建立可與之交談的子程序
my $childpid = open2($read_fh, $write_fh, 'wc');
# 提供資料
$write_fh->print("abc def\n24 187\n");
# 關閉(結束)input handle, 否則wc會一直等待
$write_fh->close();
# 取回結果(可能多行, 使用陣列)
my @wc = <$read_fh>;
# 關閉(結束)input handle, 否則wc會一直等待
$read_fh->close();
print "$_\n" for (@wc);
# 印出 2 4 15
# 等待確認子程序已結束
waitpid($childpid, 0);
exit 0;
###############################
# CP937 => Big5 轉碼
# 使用inconv指令
#
sub CP937_BIG5 {
my $eb = shift;
return '' unless $eb;
# 在家目錄/tmp下
my $big5file = $ENV{HOME} . '/tmp/big5file';
open Convert, " | iconv -s -f IBM-937 -t big5 > $big5file " or die;
print Convert $eb;
close Convert;
open B5, '<', $big5file or die;
my $big5 = <B5>;
close B5;
return $big5;
}
###############################
# CP937 => Big5 轉碼
# 使用Text::Iconv來介接inconv指令
#
use Text::Iconv;
sub CP937_BIG5 {
my $eb = shift;
if ($eb) {
return Text::Iconv->new("IBM-937" => "big5")->convert($eb);
}
else {
return '';
}
}