问题描述
显然,一个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执行。 BTW,我们使用Perl 5.12.1 for MSWin32
#!/ usr / bin / env perl
使用strict;
使用警告;
使用File :: Spec;
使用IO :: File;
use IO :: Dir;
使用功能':5.10';
my $ bash_ftp_script = File :: Spec-> catfile('bin','dm-ftp-push');
my $ fh = IO :: File-> new($ bash_ftp_script,'r')或die $ !;
my @lines = grep $ _!〜/^#.*/,< $ fh> ;;
my $ file = join'',@lines;
$ file =〜s / \\\\\
/ / 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';
说perl $ _for @files;
say for @singleLnFile;
1;
我认为你正在重定向perl脚本的输出成批处理文件? (不太熟悉Perl)
如果是这样,您必须:
perl script.pl | out-file -encoding ASCII script.bat
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;
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:
perl script.pl | out-file -encoding ASCII script.bat
这篇关于Windows PowerShell和CMD.exe中的PerlIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!