Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

Re^2: capturing command output

by Eliya (Vicar)
on Jan 25, 2012 at 10:51 UTC ( #949873=note: print w/ replies, xml ) Need Help??


in reply to Re: capturing command output
in thread capturing command output

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


Comment on Re^2: capturing command output
Select or Download Code
Re^3: capturing command output
by chrestomanci (Priest) on Jan 25, 2012 at 13:56 UTC

    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://949873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2014-04-24 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls