Zippy1970
Zippy1970

Reputation: 681

Perl: execute another program with a copy of STDIN

I have a Perl CGI script that redirects to other Perl CGI scripts. Which script is redirected to, depends on the contents of data posted with "POST".

So my redirect script reads STDIN:

read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );

examines the posted data, then redirects to another CGI script:

`run /bin/anotherscript.cgi`

This script then needs the original posted data so it also reads STDIN. But by then it's empty since it has already been read. Now I know that one solution would be to write the original contents to a file, then use this file as input for the second script:

read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );

open( DATA, ">in.txt" );
print DATA $buffer;
close( DATA );

`run /bin/anotherscript.cgi < in.txt`

Now the problem with this is that this is on a website with many users. So I would need to create unique filenames, and make sure everything is nicely cleaned up afterwards. The potential to make a mess of things, is big.

So I was wondering if there's another way to do this? Can I "push" data back to STDIN? Or is there a more direct way to pipe data to the new script?

Upvotes: 1

Views: 90

Answers (1)

ikegami
ikegami

Reputation: 386706

The easiest way:

use IPC::Run qw( run );

run [ "run", "/bin/anotherscript.cgi" ], '<', \$buf;

Upvotes: 2

Related Questions