jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have this external program that when started requires some input. For example, I could do something like
#! /usr/bin/perl open OUT,"| ./progX" or die ; print OUT "X\n\n1\n2\n\n\n" ; close OUT ;
This works just fine.
However, before progX terminates it writes some useful information to STDOUT. Normally I would do something like
open OUT,"| ./progX > output" or die ;
Than I would open the 'output' file and read the content.
This works great (as long as I have write permissions so I can create the output file).
For some reason this approach doesn't feel right. So is there a better way to catch the output directly in my perl program ?

ThnX
LuCa

Replies are listed 'Best First'.
Re: need to read/write from/to an external program
by davorg (Chancellor) on Feb 21, 2007 at 15:31 UTC
Re: need to read/write from/to an external program
by jeanluca (Deacon) on Feb 21, 2007 at 16:08 UTC
    thnx davorg

    that works great!!.
    I only don't understand how I should use the *Reader and *Writer together with 'use warnings' and 'use strict'.
    For now (without strict and warnings) I have
    #! /usr/bin/perl use FileHandle; use IPC::Open2; $pid = open2(*Reader, *Writer, "./progX" ); print Writer "X\n\n1\n2\n\n\n" ; { local $/ ; $inp = <Reader> ; } # or @inp = <Reader> close Writer, Reader ; print "Reader: $inp\n" ;

      You have the syntax for close wrong. It takes just one argument, so Reader is parsed as a bareword and not a filehandle. Simply:

      close Writer; close Reader;

      ... instead of trying to close both with one statement.

      print "Just another Perl ${\(trickster and hacker)},"
      The Sidhekin proves Sidhe did it!