Windows PowerShell 和 CMD.exe 中的 PerlIO
显然,Perl 脚本会产生两个不同的输出文件,具体取决于我是在 Windows PowerShell 还是 cmd.exe 下运行它。该脚本可以在这个问题的底部找到。文件句柄是用 IO::File 打开的,我相信 PerlIO 正在做一些奇怪的事情。与 PowerShell 相比,在 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
生成的文件是可执行的。 PowerShell 或 cmd.exe 都无法执行 UTF-16 .bat
(我认为这就是编码)。
顺便说一句,我们使用 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;
Apparently, a Perl script I have results in two different output files depending on if I run it under Windows PowerShell, or cmd.exe. The script can be found at the bottom of this question. The file handle is opened with IO::File
, I believe that PerlIO is doing some screwy stuff. It seems as if under cmd.exe
the encoding chosen is much more compact encoding (4.09 KB), as compared to PowerShell which generates a file nearly twice the size (8.19 KB). This script takes a shell script and generates a Windows batch file. It seems like the one generated under cmd.exe
is just regular ASCII (1 byte character), while the other one appears to be UTF-16 (first two bytes FF FE
)
Can someone verify and explain why PerlIO works differently under Windows Powershell than cmd.exe? Also, how do I explicitly get an ASCII-magic PerlIO filehandle using IO::File
?
Currently, only the file generated with cmd.exe
is executable. The UTF-16 .bat
(I think that's the encoding) is not executable by either PowerShell or cmd.exe.
BTW, we're using 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在将 perl 脚本的输出重定向到批处理文件中? (对 Perl 不太熟悉)
如果是这样你必须这样做:
I think you are redirecting the output of your perl script into a batch file? ( not too familiar with Perl)
If so you have to do: