什麼是正確的方式來到這裏一個美麗的輸出(所有行相同的縮進)?如何正確地將UTF-8字符串與Perl的printf對齊?
#!/usr/bin/env perl
use warnings;
use strict;
use DBI;
my $phone_book = [ [ qw(name number) ],
[ 'Kroner', 123456789 ],
[ 'Holler', 123456789 ],
[ 'Mühßig', 123456789 ],
[ 'Singer', 123456789 ],
[ 'Maurer', 123456789 ],
];
my $dbh = DBI->connect("DBI:CSV:", { RaiseError => 1 });
$dbh->do(qq{ CREATE TEMP TABLE phone_book AS IMPORT(?) }, {}, $phone_book);
my $sth = $dbh->prepare(qq{ SELECT name, number FROM phone_book });
$sth->execute;
my $array_ref = $sth->fetchall_arrayref();
for my $row (@$array_ref) {
printf "%9s %10s\n", @$row;
}
# OUTPUT:
# Kroner 123456789
# Holler 123456789
# Mühßig 123456789
# Singer 123456789
# Maurer 123456789
「使用警告;」已經存在了,當我添加「使用utf8」時,第三行看起來像這樣:「M h ig123456789」。從文件中讀取我有同樣的問題。 – 2010-01-14 16:15:49
好吧,使用「binmode STDOUT」編碼(utf8)'「也可以工作。 – 2010-01-14 16:26:17
@Dan:我啓用了警告,但我沒有收到任何警告。 – 2010-01-14 17:03:47