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

Hi Folks, I have an executable. when run it behaves in the same way as an SQL Developer. A prompt comes at which point the user has to enter the sql query and the corresponding output is displayed on the screen. I can run the executable from the perl script using the system command. But how to enter the sql query when the prompt appears on the screen through the script. As another system command would obviously work only when the previous command has been completely executed. i.e. when I exit out from the prompt. How can this be automated....... Suggestions please.

Replies are listed 'Best First'.
Re: Automate an SQL tool
by borisz (Canon) on Nov 05, 2004 at 08:05 UTC
Re: Automate an SQL tool
by pelagic (Priest) on Nov 05, 2004 at 08:07 UTC
    Deepika,
    you might consider using open3.
    Read the documentation well and have a look at the example below:
    ## file pmo3.pl use strict; use IPC::Open3; my $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH, 'perl', 's1.pl'); print WTRFH "YES or NO\n"; close WTRFH; while (<RDRFH>) { print "found this in RDRFH:\t"; print; } while (<ERRFH>) { print "found this in ERRFH:\t"; print; } exit; ## and second file: s1.pl use strict; print "this is printed to STDOUT\n"; while (<STDIN>) { chomp; print "found in STDIN of <s1.pl>: $_\n"; } warn "this is warned to STDERR\n"; exit; __OUTPUT__ !perl pmo3.pl found this in RDRFH: this is printed to STDOUT found this in RDRFH: found in STDIN of <s1.pl>: YES or NO found this in ERRFH: this is warned to STDERR

    pelagic
Re: Automate an SQL tool
by saintmike (Vicar) on Nov 05, 2004 at 08:26 UTC
    If that executable is something like mysql or a client of any other well-known database, you'll be better off using Perl's DBI module and its DBD::mysql, DBD::Pg, DBD::SQLite, etc. companions, talking to your database directly.
Re: Automate an SQL tool
by jZed (Prior) on Nov 05, 2004 at 15:35 UTC
    If what you want is a perl-based command line tool that works with almost any database backend, use dbish, the DBI interactive shell, that comes with DBI::Shell.