Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: capturing command output

by moritz (Cardinal)
on Jan 25, 2012 at 10:35 UTC ( #949869=note: print w/ replies, xml ) Need Help??


in reply to capturing command output

I'm pretty sure it does involve a shell; what other part should take care of the redirection 2>&1?

And I don't think it's any better than backticks. It also lacks proper error handling.


Comment on Re: capturing command output
Download Code
Re^2: capturing command output
by Eliya (Vicar) on Jan 25, 2012 at 10:51 UTC
    what other part should take care of the redirection 2>&1?

    Perl itself?  Like it does on Unix when 2>&1 is the only shell meta characters in the command.*

    I don't think Perl is making this optimisation on Windows, though, but I do think it's a valid question — considering that in Perl things are not always quite as obvious as it might seem at first...

    ___

    * as you can easily verify:

    $ strace -fqeexecve perl -e 'open FH, "echo foo bar 2>&1 |"; print <FH +>' execve("/usr/bin/perl", ["perl", "-e", "open FH, \"echo foo bar 2>&1 | +\"; "...], [/* 77 vars */]) = 0 [pid 1819] execve("/bin/echo", ["echo", "foo", "bar"], [/* 77 vars */ +]) = 0 foo bar

    No shell involved here.  But as soon as you add another shell metacharacter, e.g. ";", Perl will use the shell:

    $ strace -fqeexecve perl -e 'open FH, "echo foo bar ; 2>&1 |"; print < +FH>' execve("/usr/bin/perl", ["perl", "-e", "open FH, \"echo foo bar ; 2>&1 + |\""...], [/* 77 vars */]) = 0 [pid 1833] execve("/bin/sh", ["sh", "-c", "echo foo bar ; 2>&1"], [/* + 77 vars */]) = 0 foo bar

      Very interesting. ++

      I had no idea that perl did that optimization, it is good to know, but it would be nice to have a way to control it.

        but it would be nice to have a way to control it.

        You can already (indirectly) control it — at least in the direction of forcing the use of the shell.  Just add a dummy ";" to the command (which should always be possible without changing the semantics).  Or simply call sh -c "..." yourself.

        (And be sure to add a comment regarding the ";", or else the next maintainer might remove that seemingly superfluous character again... :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-09-15 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (146 votes), past polls