Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

shell redirect on windows, qx?

by doom (Deacon)
on Apr 11, 2008 at 20:22 UTC ( #679842=perlquestion: print w/ replies, xml ) Need Help??
doom has asked for the wisdom of the Perl Monks concerning the following question:

I understand there are several different flavors of perl on Windows boxes (vanilla, strawberry, active state, cygwin...), and I was wondering whether they come with anything like a Bourne shell, in particular, I was wondering how they handle redirection when shelling out with qx{} (aka backticks).

If you windows users do something like this, would you expect it to work (i.e. to give you the standard output mixed with any error messages)?

my $captured_output = qx{ some_command 2>&1 };
In fact, if you're a windows/perl person, do you think you could run the following code and report back what kind of output you see?
use File::Temp qw( tempfile ); use File::Spec qw( devnull ); $|=1; my $code =<<'CODE'; $|=1; print "ABC\n"; print STDERR "012\n"; print "IJK\n"; print STDERR "345\n"; print "XYZ\n"; print STDERR "678\n"; CODE # create a temporary perl script file my ($fh1, $scriptname) = tempfile( 'yap_XXXX', SUFFIX => '.pl'); print {$fh1} $code; close( $fh1 ); my $perl = $^X; my $devnull = File::Spec->devnull; print "No redirect: \n"; print qx{ $perl $scriptname }, "\n"; print "Redirect errors to devnull: \n"; print qx{ $perl $scriptname 2>$devnull }, "\n"; print "Redirect merging stderr and stdout: \n"; print qx{ $perl $scriptname 2>&1 }, "\n";

Comment on shell redirect on windows, qx?
Download Code
Re: shell redirect on windows, qx?
by BrowserUk (Pope) on Apr 11, 2008 at 21:01 UTC

    This is perl 5.8.6/AS811 on XP:

    c:\test>perl use File::Temp qw( tempfile ); use File::Spec qw( devnull ); $|=1; my $code =<<'CODE'; $|=1; print "ABC\n"; print STDERR "012\n"; print "IJK\n"; print STDERR "345\n"; print "XYZ\n"; print STDERR "678\n"; CODE # create a temporary perl script file my ($fh1, $scriptname) = tempfile( 'yap_XXXX', SUFFIX => '.pl'); print {$fh1} $code; close( $fh1 ); my $perl = $^X; my $devnull = File::Spec->devnull; print "No redirect: \n"; print qx{ $perl $scriptname }, "\n"; print "Redirect errors to devnull: \n"; print qx{ $perl $scriptname 2>$devnull }, "\n"; print "Redirect merging stderr and stdout: \n"; print qx{ $perl $scriptname 2>&1 }, "\n"; ^Z No redirect: 012 345 678 ABC IJK XYZ Redirect errors to devnull: ABC IJK XYZ Redirect merging stderr and stdout: ABC 012 IJK 345 XYZ 678

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Ah, cool. It looks like Active State perl behaves very much like a unix-box in this respect. Thanks for the reply.

        It looks like Active State perl behaves ...

        It's not down to AS perl. The redirection is done by the shell processor that is invoked by qx/backticks or system. Under AS perl that shell is the standard win32 cmd.exe.

        Other variants, such as cygwin builds and Strawberry etc. may choose to invoke a different shell. I think cygwin (as standard) uses a sh-alike, but you can probably get csh, bash or zsh that would run under cygwin. (If you must:), but you might have to configure something somewhere to get them used by default, and each has its own set of advantages and restrictions as with the *nix brethren.

        In theory, there is nothing to stop you from configuring a cygwin build to use cmd.exe, but I've no idea how.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: shell redirect on windows, qx?
by mr_mischief (Prior) on Apr 11, 2008 at 21:53 UTC
    The only one that comes with a bash is the Cygwin environment. Fortunately, the Windows CLI is mostly sane for redirection purposes, though. Unfortunately, I found that older versions of ActiveState might break on your code. Anything recent should be okay, though.

    To keep the response a bit shorter for those looking for just the gist, I'll put all but my newest copy of each in a readmore. I don't have Vanilla, but it's my understanding Vanilla is just a proper subset of Strawberry. I'd mostly expect similar results from those two.

    Strawberry 5.8.8:

    Strawberry Perl 5.10.0.1:

    No redirect: 012 345 678 ABC IJK XYZ Redirect errors to devnull: ABC IJK XYZ Redirect merging stderr and stdout: ABC 012 IJK 345 XYZ 678

    ActiveState 5.8.0 (build 804):

    ActiveState 5.8.8 (build 822):

    ActiveState 5.10.0 (build 1002):

    No redirect: 012 345 678 ABC IJK XYZ Redirect errors to devnull: ABC IJK XYZ Redirect merging stderr and stdout: ABC 012 IJK 345 XYZ 678

    Cygwin Perl 5.8.6:

    No redirect: 012 345 678 ABC IJK XYZ Redirect errors to devnull: ABC IJK XYZ Redirect merging stderr and stdout: ABC 012 IJK 345 XYZ 678

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://679842]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (17)
As of 2014-07-23 10:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (140 votes), past polls