Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Calling programs from perl without temp files

Reply
Thread Tools

Calling programs from perl without temp files

 
 
R Evans
Guest
Posts: n/a
 
      09-02-2004
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-02-2004
R Evans wrote:
> 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


 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      09-02-2004
In article <w0HZc.502$H26.135@trnddc07>, Jürgen Exner
<(E-Mail Removed)> wrote:

> R Evans wrote:
> > 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`;


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.
 
Reply With Quote
 
R Evans
Guest
Posts: n/a
 
      09-03-2004
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>;
----------------------------------

All the best,

Richard


Jim Gibson <(E-Mail Removed)> wrote in message news:<020920041244523982%(E-Mail Removed) >...
> In article <w0HZc.502$H26.135@trnddc07>, Jürgen Exner
> <(E-Mail Removed)> wrote:
>
> > R Evans wrote:
> > > 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`;

>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.36 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? PerlFAQ Server Perl Misc 0 04-26-2011 10:00 AM
Book: Higher-Order Perl: Transforming Programs with Programs Casey Hawthorne Perl Misc 14 07-14-2005 06:43 PM
How to initialize member reference with temp object and delete the temp object when the destructor is called? PengYu.UT@gmail.com C++ 2 05-09-2005 05:48 PM
File::Temp: opening the temp. file in "r+" mode? Also "man in the middle" A. Farber Perl Misc 3 03-03-2004 12:42 PM
Temp Internet and temp files now in winnt directory w2k pro Bibble Bobble Computer Support 1 11-27-2003 06:14 PM



Advertisments