Just a small comment, I usually use backticks when I am interested in the output of a command writing to stdout. If it does not then I think it's more appropriate to use simply system. If you don't want stderr then you can close stderr or redirect it to /dev/null (supposing unixlike here), this would be "2>&-" o "2>/dev/null".

Supposing a program writes to stderr only error conditions (and it should) seldom there is need to capture both stderr and stdout. I usually do it when there is some problem and I want to grep everything " cmd ... 2>&1 | grep ". Another case is when you want to capture the exit status of an intermediate command in a shell pipeline, while normally you would get the exit status of the last one (modern shell have a "pipefail" option -- and more-- to help that)

% stephan@ape (/home/stephan/t0) % % echo 'just another perl hacker' > secret.txt % stephan@ape (/home/stephan/t0) % % zip -P sesame safe.zip secret.txt adding: secret.txt (stored 0%) % stephan@ape (/home/stephan/t0) % % perl safe_unzip.px sesame safe.zip .cmd: [unzip -qoP sesame safe.zip 2>/dev/null] .okay: got secret file! status = [0] 1 just another perl hacker$
#!/usr/bin/perl use strict; use warnings; my $password = shift or die "args?"; my $zipfile = shift or die "zipfile?"; my $secret_file = 'secret.txt'; # use -q (silent mode) # use -o (overwrite mode) o unzip hangs if secret file already exists my $cmd = "unzip -qoP $password $zipfile 2>/dev/null"; print ".cmd: [$cmd]\n"; system($cmd); my $status = ($? >> 8); # status of cmd, like POSIX WIFEXITED() mac +ro if ($status) { die "**ERROR: unzip failed! status = [$status]"; } print ".okay: got secret file! status = [$status]\n"; -f $secret_file && print qx(cat -evnt $secret_file), "\n";

note that unzip (at least on my cygwin) returns 9 if it cannot find the zip file, and the program above gets that code correctly:

% stephan@ape (/home/stephan/t0) % % perl safe_unzip.px sesame safe1.zip .cmd: [unzip -qoP sesame safe1.zip 2>/dev/null] **ERROR: unzip failed! status = [9] at safe_unzip.px line 18.
hth --stephan

In reply to Re: Output capturing trouble by sgt
in thread Output capturing trouble by Alien

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.