Windows PowerShell 和 CMD.exe 中的 PerlIO

发布于 2024-12-17 10:05:01 字数 1280 浏览 0 评论 0原文

显然,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-12-24 10:05:01

我认为您正在将 perl 脚本的输出重定向到批处理文件中? (对 Perl 不太熟悉)

如果是这样你必须这样做:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文