in reply to Launch External Binary

A safer way is this:
open my $handle, '-|', $application, @args or confess "can't launch '$application': $!"; while (<$handle>){ # do something here } close $handle or warn "Errors while closing pipe to '$application': $! +";

The warn message after the close might give you some hints what's wrong.

Replies are listed 'Best First'.
Re^2: Launch External Binary
by Illuminatus (Curate) on Oct 09, 2008 at 14:43 UTC
    As esteemed moritz has suggested, this code is much cleaner, and includes diagnostics for the close. One thing to add; make sure that the last arg in the @arg array is '2>&1'. By default, the pipe operation of open (and shell) only sends stdout. If your application prints any information to stderr, it will be lost.

      Erm, if you call open for a pipe with a list like that it behaves akin to system in that it doesn't run the arguments through a shell so that's not really going to do anything.

      $perl -e 'open( my $x, "-|", "zsh", "-c", q{ctr=1;for i in "$@" ; do p +rint "$ctr: $i"; ctr=$((ctr + 1)); done}, "--", qw(1 2 3 4), q{2>&1} +); print while <$x>; close( $x ); print "\n"' 1: 1 2: 2 3: 3 4: 4 5: 2>&1

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        You are right -- I had forgotten about that. I think the only way to make the redirect work is to pass the command as libvenus originally specified, putting the '2>&1' right before the pipe.