Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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.

Replies are listed 'Best First'.
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?
[1nickt]: See this code. (I expected to simply eval loading threads as a check, but weirdness happened with Perlbrew so it's a grep of -V ...)
[choroba]: Config might be better than grepping -V
[Corion]: Also see Config::V, which is less of that hackery, or that hackery hidden in a module ;)
[1nickt]: The problem was with Perlbrew
[Corion]: Whoops - Config::Perl::V
[1nickt]: I found that when using Perlbrew as recommended, with cpanminus in the system perl lib, such tests were failing to detect the data about the perl that was the install destination.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-10-18 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (249 votes). Check out past polls.

    Notices?