PerlIO in Windows PowerShell and CMD.exe

蓝咒 提交于 2019-12-24 09:00:46

问题


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;

回答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


来源:https://stackoverflow.com/questions/8216027/perlio-in-windows-powershell-and-cmd-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!