How to use IPC :: Run module in perl script

I have this Perl script that connects to sqlplus database and runs the following command.

my $SQLPLUS='/opt/oracle/product/11g/db_1/bin/sqlplus -S system/coolman7@vsdb';
`$SQLPLUS \@${basePath}/VoucherQuery1.sql $startdate> ${basePath}/QueryResult1.txt`;

      

Now I am trying to run the second command via IPC :: Run. I have tried in many ways without any success. as

open (WFH1, ">", "${basePath}/QueryResult4.txt");
run('$SQLPLUS \@${basePath}/VoucherQuery4.sql $startdate', '>', \\WFH1);
close(WH1);

      

but it cannot connect to sqlplus or write to the output file. I searched on google and other forums but didn't get any solution. Please help. :)

+3


source to share


1 answer


Using lexical file descriptors makes life easier in general. They are not global and they automatically close when they go out of scope.

open (my $wfh1, ">", "${basePath}/QueryResult4.txt");

      

The problem open

may be that it failed, you are not checking if it succeeded. You can do this in two ways. Do it manually first ...

my $query_result_file = "${basePath}/QueryResult4.txt";
open (my $wfh1, ">", $query_result_file)
    or die "Couldn't open $query_result_file for writing: $!";

      

Or you can use autodie to do it for you.

use autodie;
open (my $wfh1, ">", "${basePath}/QueryResult4.txt");

      

But you don't need to manually open the file at all, IPC :: Run will do it for you if you pass the filename to it.



The next problem is what you go to run

. It is in single quotes, which means that none of the variables will be interpolated. You are literally trying to run $SQLPLUS @${basePath}/VoucherQuery4.sql $startdate

. You need double quotes.

But it's better to pass your command and arguments as a ref array, this way IPC :: Run will handle the shell quoting for you.

Here's a simple example reading a file with cat

, adding line numbers and outputting the result to a file.

use IPC::Run qw(run);
run ["cat", "-n"], "<", "/etc/passwd", ">", "test.out";

      

Putting it all together ...

my $SQLPLUS_EXE = '/opt/oracle/product/11g/db_1/bin/sqlplus';
my $SQLPLUS_DB  = 'system/coolman7@vsdb';
my @SQLPLUS_CMD = ($SQLPLUS_EXE, '-S', $SQLPLUS_DB);

run [@SQLPLUS_CMD, "\@${basePath}/VoucherQuery4.sql", $startdate],
    ">", "${basePath}/QueryResult4.txt";

      

+3


source







All Articles