我正在编写一个脚本,根据csv文件批量重命名和复制图像。csv文件包括第一列:旧名称和第二列:新名称。我想将csv文件用作perl脚本的输入,以便它检查旧名称并使用新名称将其复制到一个新文件夹中。我遇到的问题(我认为)与图像有关。它们包含像ß这样的utf8字符。当我运行脚本时,它打印出Barfu├ƒg├ñsschen,而应该是Barfußgässchen,还会出现以下错误:
Unsuccessful stat on filename containing newline at C:/Perl64/lib/File/Copy.pm line 148, <$INFILE> line 1.
Copy failed: No such file or directory at X:\Script directory\correction.pl line 26, <$INFILE> line 1.
我知道这与Binmode utf8有关,但即使我尝试一个简单的脚本(在这里看到它:How can I output UTF-8 from Perl?):
use strict;
use utf8;
my $str = 'Çirçös';
binmode(STDOUT, ":utf8");
print "$str\n";
它输出的是这个:Ãirþ÷s
这是我的整个脚本,有人能解释一下我错在哪里吗?(因为我在测试东西,所以代码不是很干净)。
use strict;
use warnings;
use File::Copy;
use utf8;
my $inputfile = shift || die "give input!\n";
#my $outputfile = shift || die "Give output!\n";
open my $INFILE, '<', $inputfile or die "In use / not found :$!\n";
#open my $OUTFILE, '>', $outputfile or die "In use / not found :$!\n";
binmode($INFILE, ":encoding(utf8)");
#binmode($OUTFILE, ":encoding(utf8)");
while (<$INFILE>) {
s/"//g;
my @elements = split /;/, $_;
my $old = $elements[1];
my $new = "new/$elements[3]";
binmode STDOUT, ':utf8';
print "$old | $new\n";
copy("$old","$new") or die "Copy failed: $!";
#copy("Copy.pm",\*STDOUT);
# my $output_line = join(";", @elements);
# print $OUTFILE $output_line;
#print "\n"
}
close $INFILE;
#close $OUTFILE;
exit 0;
use utf8
编译指示告诉Perl你的源代码是用utf8编写的,它不涉及数据。 - simbabqueen_GB.UTF-8
和Putty设置为UTF-8时,您的第一个代码片段可以正常工作。我是在同一个shell中使用VIM创建该文件的。 - Alastair McCormack