Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Transparent Wrapper

by devslashneil (Friar)
on Mar 05, 2004 at 12:02 UTC ( [id://334191]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
I was wondering what the best way to write a perl script which acts a transparent wrapper to another command is.

ie. All lines of input from the user are passed to the program, and all lines of output from the program are returned to the user.

Also i was wondering if it would then be possible to replace the running process i.e the script, with the actual process which it was communicating with, so the script terminates and the real command continues running.

Any help would be much appreciated :)

Neil Archibald

Replies are listed 'Best First'.
Re: Transparent Wrapper
by Happy-the-monk (Canon) on Mar 05, 2004 at 12:13 UTC
    Term::Shell or the like could help with the user interface if you are imagining a shell there.
      Urmm i'd rather not use any additional modules, i wanted to try get it working with a unidirectional pipe? Am i way off here?

      Neil Archibald

        Perl offers you the means to do it:
        chomp ($input = <>); to catch User input.
        print STDOUT $output; to print output. (Leave out the "STDOUT" if you like)
        print STDERR $output; to print errors.

        You may use open() to create filehandles on a pipe to or from your other process and print to it or read from it. You can also use open() to access STDERR or even STDOUT to read from. You might select a filehandle just for lazyness' sake. Perl enables you to it the hard way a little less awkward than I could imagine. You will learn a lot.
        Or you could just save your time with some CPAN module.

Re: Transparent Wrapper
by matija (Priest) on Mar 05, 2004 at 12:35 UTC
    I think Expect is the module you should look at.

    It supports a lot of the stuff you are looking for as it regards communicating with the underlying process.

    Unfortunately, I doesn't support terminating the controling program and reconnecting the underlying process with the terminal.

    In fact, I don't know of anything that does that. Expect-the-language (as opposed to Expect-the-module) has a command that stops the program looking at the input, and just makes sure the data passes from one to the other and back unimpeded, but even then you still have both your Expect script and the whole Expect interpreter still loaded in memory.

      Is it possible to do the first part without using a module though? Using unidirectional pipes? :)

      Neil Archibald
        Can I ask why non-standard modules are a problem? They should not be. If you can provide info, we can probably help you get a module into your environment (i.e. non-root access, not sure of how to use modules on Win32, etc).
Re: Transparent Wrapper
by castaway (Parson) on Mar 05, 2004 at 13:02 UTC
    I think you're looking for "Bidirectional Communication with Another Process" in perlipc, there are standard internal functions to read from and write to the same subprocess, open2 and open3.

    C.

Re: Transparent Wrapper
by bmann (Priest) on Mar 05, 2004 at 15:50 UTC
    I read your question a little differently than the others.

    Seems to me you're simply looking for exec, which executes a command and never returns.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://334191]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found