karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:
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»
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: IPC::System::Simple: Why isn't my exception caught?
by kschwab (Vicar) on Nov 20, 2013 at 14:01 UTC | |
by karlgoethebier (Abbot) on Nov 20, 2013 at 14:48 UTC | |
Re: IPC::System::Simple: Why isn't my exception caught?
by stefbv (Priest) on Nov 20, 2013 at 13:56 UTC | |
by karlgoethebier (Abbot) on Nov 20, 2013 at 14:45 UTC | |
Re: IPC::System::Simple: Why isn't my exception caught?
by ikegami (Patriarch) on Nov 20, 2013 at 17:36 UTC | |
by karlgoethebier (Abbot) on Nov 20, 2013 at 20:24 UTC |