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.


In reply to Re^4: STDOUT STDERR and response code from external Linix command by Eliya
in thread STDOUT STDERR and response code from external Linix command by gdanenb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.