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

I want to execute an interactive command from perl script that requires user input. When I try to run it, it hangs and I have to do Ctrl^c to exit. Is there a way that I can achive this functionality? Thanks in advance

Replies are listed 'Best First'.
Re: Execute Interactive commands
by graff (Chancellor) on Jun 17, 2008 at 03:47 UTC
    The previous replies might be good guesses about the nature of your goal, or about what you're doing wrong, but your main problem is that you haven't told us enough about what you are actually doing (apart from what you are trying to do, which is also a bit vague).

    What sort of interactive command are you trying to run? (Hint: if it has anything to do with logging into some remote host, that affects what the solution ought to be.)

    Is it your intention that the person running the perl script is supposed to type things (after the perl script starts the "interactive command") and these things are then conveyed to (acted on by) the child command process? If so, what sort of value is the perl script supposed to be adding (that is, why use a perl script at all, rather than just running the "interactive command" directly)? If not, just what are you trying to accomplish?

    You might need IPC::Open2 (or Open3), or you might need Expect. When you us only vague information, we can only give you back vague advice.

Re: Execute Interactive commands
by tachyon-II (Chaplain) on Jun 17, 2008 at 02:37 UTC

    You are presumably trying to do this using `backtics`. Bacticks capture the scripts output into a variable. In fact the "hang" is the program you are running waiting patiently for your input (with the prompt captured by the backtics):

    C:\>perl -e "print 'Hello '; system('date'); print 'world'"; Hello The current date is: Tue 17/06/2008 Enter the new date: (dd-mm-yy) 17-06-08 world C:\> C:\>perl -e "print 'Hello '; `date`; print 'world'"; Hello 18-06-08 <--- type in data here world C:\>date The current date is: Wed 18/06/2008 [snip] C:\>perl -e "print 'Hello '; $p = `date`; print $p.' world'"; Hello 17-08-08 The current date is: Tue 17/06/2008 Enter the new date: (dd-mm-yy) world C:\>
Re: Execute Interactive commands
by jettero (Monsignor) on Jun 17, 2008 at 00:35 UTC
    Yeah, system should do what you want. That or, possibly, an exec.

    -Paul

Re: Execute Interactive commands
by apl (Monsignor) on Jun 17, 2008 at 09:53 UTC
    Could you show us a minimal program that demonstrates the problem?
Re: Execute Interactive commands
by zentara (Cardinal) on Jun 17, 2008 at 11:43 UTC
    Here is a simple example running bc interactively thru IPC::Open3. The IO::Select is an added frill, you can do without it if you want real simplicity.
    #!/usr/bin/perl # It's only drawback is it only outputs 1 line of bc output # so it errs on something like 234^12345 (which outputs a big number) use warnings; use strict; use IPC::Open3; use IO::Select; #interface to "bc" calculator my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\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 CandyGram for Mongo
Re: Execute Interactive commands
by dwm042 (Priest) on Jun 17, 2008 at 16:35 UTC
    A simple example of this kind of program is:

    #!/usr/bin/perl use warnings; use strict; print "Enter a search pattern.\n"; my $info=<STDIN>; chomp($info); if ( $info =~ m/\w+/ ) { system("dir $info"); } else { print "You didn\'t enter text.\n"; }
    And an example run is:

    C:\Info>perl search.pl Enter a search pattern. why* Volume in drive C has no label. Volume Serial Number is ACCF-791E Directory of C:\Info 02/25/2008 07:40 PM 175 whydont.pl 1 File(s) 175 bytes 0 Dir(s) 51,114,610,688 bytes free