Calling programs from perl without temp files

Discussion in 'Perl' started by R Evans, Sep 2, 2004.

  1. R Evans

    R Evans Guest

    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
    R Evans, Sep 2, 2004
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Sep 2, 2004
    #2
    1. Advertising

  3. R Evans

    Jim Gibson Guest

    In article <w0HZc.502$H26.135@trnddc07>, Jürgen Exner
    <> 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.
    Jim Gibson, Sep 2, 2004
    #3
  4. R Evans

    R Evans Guest

    Re: Calling programs from perl without temp files - problem solved

    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 <> wrote in message news:<020920041244523982%>...
    > In article <w0HZc.502$H26.135@trnddc07>, Jürgen Exner
    > <> 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.
    R Evans, Sep 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    555
    Mark P
    May 9, 2005
  2. A. Farber
    Replies:
    3
    Views:
    236
    Ben Morrow
    Mar 3, 2004
  3. Casey Hawthorne
    Replies:
    14
    Views:
    430
  4. pgodfrin
    Replies:
    12
    Views:
    143
  5. PerlFAQ Server
    Replies:
    0
    Views:
    242
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page