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

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

by gdanenb (Acolyte)
on Jan 05, 2012 at 06:41 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://946329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (11)
As of 2024-04-23 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found