Calling programs from perl without temp files

R

R Evans

Hello,

In a Perl script, I run a program, Program.pl, by using:

@output = `cat $file_name | Program.pl`;

However I want to run this program when the contents of the file named
$file_name are stored in a string variable called $file_contents, as I
don't have permission to store them in a file. So I want to capture
something like:

@output = `cat $file_contents | Program.pl`;

Does anyone know how this can be done?

Many thanks for your help,

Richard
 
J

Jürgen Exner

R said:
Hello,

In a Perl script, I run a program, Program.pl, by using:

@output = `cat $file_name | Program.pl`;

Useless use of cat. Why not
Program.pl < $file_name
However I want to run this program when the contents of the file named
$file_name are stored in a string variable called $file_contents, as I
don't have permission to store them in a file. So I want to capture
something like:

@output = `cat $file_contents | Program.pl`;

Does anyone know how this can be done?

You mean something like

@output = `echo $file_contents | Program.pl`;


jue
 
J

Jim Gibson

Jürgen Exner said:
Useless use of cat. Why not
Program.pl < $file_name


You mean something like

@output = `echo $file_contents | Program.pl`;

I think the poster means that $file_contents contains the _contents_ of
the file, not the name (i.e. { local $/; $file_contents = <FILE> } ).
In that case, you can do something like (untested):

open(EXE,'|Program.pl') or die $!;
print EXE $file_contents;

See 'perldoc -f open' and 'perldoc perlipc'. If you also want to
capture the output of the program, then you need to see IPC::Open2 and
IPC::Open3, which may not be supported on all platforms.
 
R

R Evans

Hi,

Thanks for pointing me to Open2. I got what I wanted using:

----------------------------------
use IPC::Open2;
$pid = open2(\*RDRFH, \*WTRFH, 'perl Program.pl');
print WTRFH $file_contents;
close(WTRFH);
@output=<RDRFH>;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top