简体   繁体   English

Windows PowerShell和CMD.exe中的PerlIO

[英]PerlIO in Windows PowerShell and CMD.exe

Apparently, a Perl script I have results in two different output files depending on if I run it under Windows PowerShell, or cmd.exe. 显然,我有一个Perl脚本导致两个不同的输出文件,具体取决于我是否在Windows PowerShell或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. 使用IO::File打开文件句柄,我相信PerlIO正在做一些棘手的事情。 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). 看起来好像在cmd.exe下,选择的编码是更紧凑的编码(4.09 KB),而PowerShell生成的文件几乎是两倍大小(8.19 KB)。 This script takes a shell script and generates a Windows batch file. 此脚本采用shell脚本并生成Windows批处理文件。 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 ) 似乎在cmd.exe下生成的那个只是常规ASCII(1个字节字符),而另一个看起来是UTF-16(前两个字节FF FE

Can someone verify and explain why PerlIO works differently under Windows Powershell than cmd.exe? 有人可以验证并解释为什么PerlIO在Windows Powershell下的工作方式与cmd.exe不同吗? Also, how do I explicitly get an ASCII-magic PerlIO filehandle using IO::File ? 另外,如何使用IO::File显式获取ASCII-magic PerlIO文件句柄?

Currently, only the file generated with cmd.exe is executable. 目前,只有使用cmd.exe生成的文件是可执行的。 The UTF-16 .bat (I think that's the encoding) is not executable by either PowerShell or cmd.exe. UTF-16 .bat (我认为这是编码)不能由PowerShell或cmd.exe执行。

BTW, we're using Perl 5.12.1 for MSWin32 顺便说一句,我们使用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? 我认为您将perl脚本的输出重定向到批处理文件中? ( not too familiar with Perl) (不太熟悉Perl)

If so you have to do: 如果是这样,你必须这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM