簡體   English   中英

Windows PowerShell和CMD.exe中的PerlIO

[英]PerlIO in Windows PowerShell and CMD.exe

顯然,我有一個Perl腳本導致兩個不同的輸出文件,具體取決於我是否在Windows PowerShell或cmd.exe下運行它。 該腳本可以在這個問題的底部找到。 使用IO::File打開文件句柄,我相信PerlIO正在做一些棘手的事情。 看起來好像在cmd.exe下,選擇的編碼是更緊湊的編碼(4.09 KB),而PowerShell生成的文件幾乎是兩倍大小(8.19 KB)。 此腳本采用shell腳本並生成Windows批處理文件。 似乎在cmd.exe下生成的那個只是常規ASCII(1個字節字符),而另一個看起來是UTF-16(前兩個字節FF FE

有人可以驗證並解釋為什么PerlIO在Windows Powershell下的工作方式與cmd.exe不同嗎? 另外,如何使用IO::File顯式獲取ASCII-magic PerlIO文件句柄?

目前,只有使用cmd.exe生成的文件是可執行的。 UTF-16 .bat (我認為這是編碼)不能由PowerShell或cmd.exe執行。

順便說一句,我們使用Perl 5.12.1 for 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;

我認為您將perl腳本的輸出重定向到批處理文件中? (不太熟悉Perl)

如果是這樣,你必須這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM