Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Interacting With A Program Over Stdin/Exec

by arunhorne (Pilgrim)
on Jun 23, 2004 at 09:58 UTC ( [id://368989]=perlquestion: print w/replies, xml ) Need Help??

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

Monks

I am working on writing a Perl wrapper around the command line control utilities of a server. It is a log in "shell" essentially, so you start it with some basic information such as host and port and it provides you with a prompt, as so:

> {0}:

What I need to do now is send command in to this prompt sequentially, and potentially conditionally depending on logic in my wrapper. E.g. if I want to send the commands "start" and then "exit", thus if I typed it the prompt would look as follows:

> {0}: start

> {1}: exit

Is there any way in Perl I can achieve this, i.e. mimick manual input on stdin. If there is a way that uses only very core perl modules/core functionality that would be preferable as we only have a relatively basic installation of Perl and I cannot install new modules, but any help would be greatly appreciated.

Kind regards,

____________
Arun

Replies are listed 'Best First'.
Re: Interacting With A Program Over Stdin/Exec
by borisz (Canon) on Jun 23, 2004 at 10:04 UTC
    Not in the core, but Expect can do that.
    Boris
Re: Interacting With A Program Over Stdin/Exec
by thospel (Hermit) on Jun 23, 2004 at 10:42 UTC
    Sounds like you want Term::ReadLine (or one of its CPAN submodules) at least for the part where you present a prompt to the user.

    How to send input to your slave programs is a completely separate issue. That might be as simply as piping a stream into them or as hard as needing pseudoterminals and select on all the I/O streams.

    Also notice that you don't have to be root to install modules. You can have a private module repository anywhere you can store files.

Re: Interacting With A Program Over Stdin/Exec
by zentara (Archbishop) on Jun 23, 2004 at 12:49 UTC
    Well I know you said you needed basic Perl , with no modules, but IPC3 does alot of what you want.
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; #interface to "bc" calculator #my $pid = open3(\*WRITE, \*READ, \*ERROR,"bc"); my $pid = open3(\*WRITE, \*READ,0,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; select(undef,undef,undef,2); #get the answer from bc chomp(my $answer = <READ>); print "$query = $answer\n"; } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-20 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found