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

I have a program "execopu -text" that can take input from a file "file.txt" and execute the data inside by doing: execopu -text < file.txt from a Unix command line.

"file.txt" looks like this:
cell u csno = 7 ccuoosmin_3g1x = "" ccuoos_3g1x = "" u u csno = 12 ccuoosmin_3g1x = "" ccuoos_3g1x = "" u

I want to automate the generation of the "file.txt" and execution of the script into one Perl script. How can I modify this script to "<" redirect the output generated by the "print" statements using the same method as the Unix "<" but do this within perl instead? Right now the sript below is having issues using the "open" command.
#!/usr/bin/perl my @sitenumbers = (7,12); open(EXECOPU, '| execopu -text') or die("Can't run execopu: $!\n"); print EXECOPU "cell\n"; foreach my $site_number (@sitenumbers){ print EXECOPU <<"EOF"; u csno = $site_number ccuoosmin_3g1x = "" ccuoos_3g1x = "" u EOF }

Replies are listed 'Best First'.
Re: Redirect Unix Input Within Perl
by ikegami (Patriarch) on Apr 20, 2005 at 14:36 UTC

    The script provided already does exactly that. It can be proven by changing
    open(EXECOPU, '| execopu -text')
    to
    open(EXECOPU, '| cat')

    $ cat 449609.pl #!/usr/bin/perl my @sitenumbers = (7,12); #open(EXECOPU, '| execopu -text') open(EXECOPU, '| cat') or die("Can't run execopu: $!\n"); print EXECOPU "cell\n"; foreach my $site_number (@sitenumbers){ print EXECOPU <<"EOF"; u csno = $site_number ccuoosmin_3g1x = "" ccuoos_3g1x = "" u EOF } $ perl 449609.pl cell u csno = 7 ccuoosmin_3g1x = "" ccuoos_3g1x = "" u u csno = 12 ccuoosmin_3g1x = "" ccuoos_3g1x = "" u
Re: Redirect Unix Input Within Perl
by polettix (Vicar) on Apr 20, 2005 at 14:41 UTC
    awohld, why repeating node Auto Feeding Data to Unix Programs?

    Anyway, Right now the sript below is having issues using the "open" command: what are these issues? Is the open working? Does the script die, and what's the error message?

    Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

    Don't fool yourself.
      The script was working fine after all, it was a syntax issue with the text generation. Can't have spaces besides the "=". Thanks for all the help!
Re: Redirect Unix Input Within Perl
by gellyfish (Monsignor) on Apr 20, 2005 at 14:36 UTC

    What do you mean by "issues using the "open" command." ? If it is failing you should be getting a meaningful message from $?. Anyway if you want to capture the output of the program as well as piping to it's STDIN you probably want to look at the module IPC::Open2 which is standard with Perl.

    /J\

      The "execopu" program is complaing about null fields and mask problems when I use the Perl script. I have no idea what those are because there's no documentation about that. But "execopu" is working usiung the "execopu -text < file.txt" method, but not with the Perl script. ???