in reply to How to handel sqlplus error in perl script.

I'd suggest you use a module to call the external command, not backticks. There are several modules that give you the ability to capture and redirect a command's STDOUT and STDERR: Capture::Tiny, IPC::Run3, and IPC::Run, the latter two even allow for you to feed custom STDIN to the command. Then you won't have to deal with the additional layer of the shell and you can do everything directly in Perl. Also, shell interpolation can introduce security holes.

Another thing, which is a matter of taste: I usually don't redirect my program's STDOUT and STDERR. If I want a logfile, I open it and write to it specifically (print $logfh "whatever\n";), and if my program produces any other output I let whoever is calling my Perl script deal with it (cron, a daemon runner, the user on the terminal, etc.).

Replies are listed 'Best First'.
Re^2: How to handel sqlplus error in perl script.
by Ankur_kuls (Sexton) on Dec 24, 2014 at 06:19 UTC

    Thanks all for your inputs.. I have installed IPC::Run on my machine... I have googled but not able to find exactly how to run it. I am running below command

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

    instead of the existing

     `$SQLPLUS \@${basePath}/VoucherQuery4.sql $startdate> ${basePath}/QueryResult4.txt`

    but receiving error

    Global symbol "$WFH1" requires explicit package name at VoucherStateC +hange.pl_other2 line 47. Global symbol "$WFH1" requires explicit package name at VoucherStateCh +ange.pl_other2 line 48. Execution of VoucherStateChange.pl_other2 aborted due to compilation e +rrors.

    Feeling myself clueless...Could you please help me here or provide any good documentation on this module? thanks

      Write like this
      open my($filehandle) ... run ... $filehandle

        Hi, I tried what you what suggested.. I also tried the below..

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

        These are not throwing any error but not able to write on the output file.. could you please extend your help? sorry for sounding dumb and confused.. :)