Hi all,

I got stuck with this snippet:

#!/usr/bin/perl use IPC::System::Simple qw( capture ); use strict; use warnings; my $result; my $file = $0; eval{ $result = capture("cat $file | wc -l"); }; if ($@) {print "Error: $@"} print qq(capture $file: $result); $file = qq(foo); undef $result; eval{ $result = capture("cat $file | wc -l"); }; if ($@) {print "Error: $@"} print qq(capture $file: $result); __END__ ./test.pl capture ./test.pl: 32 cat: foo: Datei oder Verzeichnis nicht gefunden capture foo: 0

File foo doesn't exist, but the error i get ist from cat:

cat: foo: Datei oder Verzeichnis nicht gefunden

What do is miss?

Update:

I've been playing around a bit with IPC::Run, here is what i did:

OK, it's still the same idiotic shell command, but for practicing purposes it's good.

#!/usr/bin/perl use strict; use warnings; use IPC::Run qw( run harness ); use Try::Tiny; use Carp; my $file = shift || $0; my @cat = ( "cat", $file ); my @wc = qw( wc -l ); my ( $error, $result ); my @command = ( \@cat, '|', \@wc, '1>', \$result, '2>', \$error ); my $harness = harness @command; try { run $harness; croak $error if $harness->full_result; } catch { print qq(Error: $_); }; chomp $result; printf( "%s:\t%s\n", "result", $result ); printf( "%s:\t%s\n", "cat", $harness->full_result(0) ); printf( "%s:\t%s\n", "wc", $harness->full_result(1) ); __END__

Best regards and thanks for any help, Karl

P.S.: Yes, i know the command is idiotic, i could say wc -l file...it's just for testing.

«The Crux of the Biscuit is the Apostrophe»


In reply to IPC::System::Simple: Why isn't my exception caught? by karlgoethebier

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.