顯然,我有兩個不同的輸出文件導致Perl腳本運行,這取決於我是否在Windows PowerShell或cmd.exe下運行它。腳本可以在這個問題的底部找到。文件句柄用IO::File
打開,我相信PerlIO正在做一些棘手的事情。看起來好像在cmd.exe
之下,與PowerShell相比,選擇的編碼更加緊湊(4.09 KB),而PowerShell可以生成近兩倍大小的文件(8.19 KB)。該腳本需要一個shell腳本並生成一個Windows批處理文件。好像cmd.exe
下產生的一種是隻是普通的ASCII(1個字節字符),而另一個似乎是UTF-16(前兩個字節FF FE
)Windows PowerShell和CMD.exe中的PerlIO
有人能證實並解釋原因PerlIO的工作方式不同下的Windows PowerShell比cmd.exe?另外,如何使用IO::File
明確獲取ASCII-magic PerlIO文件句柄?
目前,只有cmd.exe
生成的文件是可執行文件。 UTF-16 .bat
(我認爲這是編碼)不能由PowerShell或cmd.exe執行。
順便說一句,我們使用Perl 5.12.1爲MSWin32
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
use IO::File;
use IO::Dir;
use feature ':5.10';
my $bash_ftp_script = File::Spec->catfile('bin', 'dm-ftp-push');
my $fh = IO::File->new($bash_ftp_script, 'r') or die $!;
my @lines = grep $_ !~ /^#.*/, <$fh>;
my $file = join '', @lines;
$file =~ s/ \\\n/ /gm;
$file =~ tr/'\t/"/d;
$file =~ s/ +/ /g;
$file =~ s/\b"|"\b/"/g;
my @singleLnFile = grep /ncftp|echo/, split $/, $file;
s/\$PWD\///g for @singleLnFile;
my $dh = IO::Dir->new('.');
my @files = grep /\.pl$/, $dh->read;
say 'echo off';
say "perl $_" for @files;
say for @singleLnFile;
1;
看一看上的超級用戶這個非常類似的問題:http://superuser.com/questions/232075/powershell-overruling -perl-binmode – Christopher