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

Worthy monks please forgive this intrusion by a novice seeking wisdom ...

I have arrived at a new chapter and been given responsibility for maintaining a perl script. This script is a long and faithful servant that does various things but at it's core it executes various system commands on a linux box

To facilitate this it makes use of use IO::CaptureOutput qw(capture_exec_combined);

The code does the following:

my @command = ( $command, $param1, $param2, ) my ( $stdout, $success, $exit_code ) = capture_exec_combined(@command) +;

This works for all the existing commands that we have because they can all be mapped into the necessary elements

So for example (simplified for the purpose of demonstration):

Linux 'ls file*.txt' becomes $command = 'ls', $param1 = 'file*.txt' and it does all the right things (i.e. $stdout contains a list of files that match files*.txt)

I now have to call a command in a database that can only be called in one of these two ways:

cat file.sql | sql database -uUsername -Ppassword

or

sql database -uUsername -Ppassword <file.sql

i.e. the necessary input can only come from stdin.

Obviously $command = 'sql', $param1 = 'database', $param2 = '-uUsername' and $param3 = '-Ppassword' (that bit is easy and works) but how do I feed it the contents of the file?

Is there a way to achieve this short of re-writing the existing framework in its entirety because the existing script is significantly larger, does everything else really well and this is only a minor but necessary modification?

As I said I am a novice supplicant seeking wisdom - thank you.

Replies are listed 'Best First'.
Re: Using STDIN with IO::CaptureOutput
by ikegami (Patriarch) on Jan 09, 2012 at 19:35 UTC
    cat file.sql | sql database -uUsername -Ppassword

    or the cheaper

    sql database -uUsername -Ppassword <file.sql

    are shell commands. You need a shell to execute them.

    my $shell_cmd = 'sql database -uUsername -Ppassword <file.sql'; capture_exec_combined('sh', '-c', $shell_cmd)

    If you pass a single argument to capture_exec_combined, it might implicitly invoke a shell (like system does). If so, the following would suffice:

    my $shell_cmd = 'sql database -uUsername -Ppassword <file.sql'; capture_exec_combined($shell_cmd)
Re: Using STDIN with IO::CaptureOutput
by Xiong (Hermit) on Jan 09, 2012 at 23:00 UTC

    Previously I wrestled with IO::Capture* and friends, with unsatisfactory results. The module is substandard, the author unresponsive.

    I have switched to Test::Trap, with excellent results. It need not be used solely in a testing framework. You may be interested in Cheat::Sheet::Test for concise usage. Note accessor methods such as $got = $trap->stdout;

    I'm not the guy you kill, I'm the guy you buy. —Michael Clayton
Re: Using STDIN with IO::CaptureOutput
by jfroebe (Parson) on Jan 09, 2012 at 20:52 UTC

    You're question is already answered but for your particular issue, check the parameter options for your 'sql' program. There might very well be an input_file option (typically '-i <filename>' or @filename).. or there might be a way to load the file interactively from within 'sql' : loadfile '<filename>'.

    Jason L. Froebe

    Blog, Tech Blog