PerlIO in Windows PowerShell and CMD.exe
Asked Answered
E

1

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;
Exegetic answered 21/11, 2011 at 17:38 Comment(1)
Have a look at this very similar question on superuser: superuser.com/questions/232075/…Hewlett
I
4

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
Isostasy answered 21/11, 2011 at 19:4 Comment(5)
I am doing that.. so what would be the cause of this? why does redirection work differently in one shell then in another?Exegetic
Here is a good reference question! Thanks: #1707897Exegetic
@EvanCarroll PowerShell uses unicode by default and if you just use > or out-file ( without specifying encoding), the file will not be ASCII. So you have to specify the encoding explicitly. cmd by default creates acsii files.Isostasy
Right, which would almost be OK if .bat files executed by at least PowerShell would accept unicode encoding. What a horrible oversight.Exegetic
@EvanCarroll - Yeah, but if you write a ps1 file like that, you can execute in Powershell. I guess bat is still being handled by some older part.Isostasy

© 2022 - 2024 — McMap. All rights reserved.