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

I'm looking at feeding some data to an external program from within Perl. Unfortunately, it is not as simple as sendng some variables to the program and letting it do its magic. I'd like to send the data with a HERE document. Has anyone done this? Thoughts?

the code would look like:

open (EXTPROGRAM, "|$externalprogram") or die "Cannot fork to $externa +lprogram: $!"; print LOG <<EOJ; print EXTPROGRAM <<EOJ; (lines to send to external program) EOJ

If anyone has done this, might be helpful to here if this is even possible (don't want to resort to a shell script). I'm not at present able to actually test this out to see if it will work, although perl -c tells me that my syntax is clean :)

TIA

Replies are listed 'Best First'.
Re: Feeding data to external program
by sauoq (Abbot) on Jan 24, 2003 at 21:42 UTC

    That should work fine. It gets a little more difficult when you want to send data to the external program and get data back. If you need to do that, you might start by looking at IPC::Open2 and reading perldoc perlipc.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Feeding data to external program
by Mr. Muskrat (Canon) on Jan 24, 2003 at 22:08 UTC

    Perhaps you should look into IO::Tee since you are trying to write to two different filehandles at once.

    The following is to be considered psuedocode since I don't have your full source code:

    use IO::Tee; my $tee = IO::Tee->new("|$externalprogram",">>$log"); print $tee <<EOJ; (lines to send to external program) EOJ

    Note: you may have to use it in conjunction with IO::File.

      Thanks, unfortunately, we don't have the IO modules loaded, may have to sweet talk the admin's into installing it if I can't get an effective workaround (we're using an older version of Perl 5). But at least I know I'm on the right track.
Re: Feeding data to external program
by poj (Abbot) on Jan 25, 2003 at 12:44 UTC
    I can't see how this will work
    print LOG <<EOJ; print EXTPROGRAM <<EOJ;
    because the second line is printed to the log and not executed. If you want a log then perhaps use something like this
    my @data=<<EOJ; (line1 to send to external program) (line2 to send to external program) (line3 to send to external program) EOJ for (@data){ print LOG; print EXTPROGRAM; }
    poj
Re: Feeding data to external program
by Aristotle (Chancellor) on Jan 26, 2003 at 00:03 UTC
    print $_ <<"EOJ" for *LOG, *EXTPROGRAM; foo $bar @baz EOJ
    Note you should always quote your heredoc terminators.

    Makeshifts last the longest.