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;
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • Have a look at this very similar question on superuser: http://superuser.com/questions/232075/powershell-overruling-perl-binmode – Christopher Nov 21 '11 at 17:50

1 Answers1

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
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • I am doing that.. so what would be the cause of this? why does redirection work differently in one shell then in another? – Evan Carroll Nov 21 '11 at 19:07
  • Here is a good reference question! Thanks: http://stackoverflow.com/questions/1707397/powershell-using-redirection-within-the-script-produces-a-unicode-output-how-t – Evan Carroll Nov 21 '11 at 19:10
  • @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. – manojlds Nov 21 '11 at 19:11
  • Right, which would almost be OK if `.bat` files executed by at least PowerShell would accept unicode encoding. What a horrible oversight. – Evan Carroll Nov 21 '11 at 19:16
  • 1
    @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. – manojlds Nov 21 '11 at 19:53