2011-11-21 43 views
1

顯然,我有兩個不同的輸出文件導致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 FEWindows 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; 
+0

看一看上的超級用戶這個非常類似的問題:http://superuser.com/questions/232075/powershell-overruling -perl-binmode – Christopher

回答

4

我想你重定向你的Perl腳本輸出到一個批處理文件? (不太熟悉Perl)

如果是這樣,你需要做的:

perl script.pl | out-file -encoding ASCII script.bat 
+0

我正在那樣做..那麼這是什麼原因呢?爲什麼重定向在一個shell中和另一個shell中的工作方式不同? –

+0

這是一個很好的參考問題!謝謝:http://stackoverflow.com/questions/1707397/powershell-using-redirection-within-the-script-produces-a-unicode-output-how-t –

+0

@EvanCarroll PowerShell默認使用unicode,如果你只是使用'>'或'out-file'(不指定編碼),文件將不是ASCII。所以你必須明確指定編碼。 cmd默認創建acsii文件。 – manojlds