Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Think about Loose Coupling
 
PerlMonks  

Re^3: STDOUT STDERR and response code from external Linix command

by gdanenb (Acolyte)
on Jan 05, 2012 at 06:41 UTC ( #946329=note: print w/ replies, xml ) Need Help??


in reply to Re^2: STDOUT STDERR and response code from external Linix command
in thread STDOUT STDERR and response code from external Linix command

Could you pls explain why I need open one more shell?

Do I need to do the same when calling external scripts(perl,python) ?


Comment on Re^3: STDOUT STDERR and response code from external Linix command
Re^4: STDOUT STDERR and response code from external Linix command
by Eliya (Vicar) on Jan 05, 2012 at 11:01 UTC

    There is no extra shell being called.   While it's normally the shell that does the 2>&1 redirection, Perl is doing some optimisations to not call the shell when it can be avoided.  For example, in the above case, when you say my $out = `date 2>&1`, Perl does the redirection itself, and execs date directly.  When you, however, specify an executable which isn't found, nothing is being executed at all — which is why you don't get the shell's error message in this case.

    In case you want to see the shell's error message no matter what, you have to either call it explicitly (as done above), or put something in the command line that keeps Perl from making the optimisation, e.g. a semicolon (shell meta-character).  In other words, the following two snippets would have the same net effect

    my $out = `sh -c dat 2>&1`; my $out = `dat ; 2>&1`; # prevent optimisation

    i.e. in both cases you'd capture the error message of the shell.  While in this case

    my $out = `dat 2>&1`;

    nothing would be executed at all, and consequently, nothing captured either.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-04-23 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls