in reply to Question on inline redirection
First, you should know that double quotes interpolate, and Perl will try to interpolate an array named @myserver with code that says "sqlplus.exe -s uid/pwd@myserver". To avoid the interpolation, you can either put a backslash in front of the at sign to escape it, or use single quotes which don't interpolate ('sqlplus.exe -s uid/pwd@myserver' or "sqlplus.exe -s uid/pwd\@myserver"). If you Use strict and warnings, that will catch a mistake like that and lots of others.
Second, it looks as if you're trying to pipe data to this program. In Perl, you'd do that with open like so:
open my $pipe_fh, '|-', 'sqlplus.exe -s uid/pwd@myserver' or die "Can't open pipe: $!";
Then you can print a heredoc into the pipe:
print {$pipe_fh} <<'END_OF_SQL' select * from emp; exit; END_OF_SQL ;
Note that I put single quotes around the heredoc terminator to make it not interpolate. The default is interpolation, which you also get if you put the terminator in double quotes.
If you're using __DATA__, that could look like this:
print {$pipe_fh} <DATA>;
When you close the pipe, Perl will wait for the program to finish, and you get its exit status in $?.
close $pipe_fh or die "close failed: $!"; if ( $? ) { die "sqlplus returned non-zero exit status '$?'"; }
If you use English, you can refer to $? as $CHILD_ERROR instead, which is a bit more readable.
Update: Here's all the code I suggested together in one block:
use strict; use warnings; use English '-no_match_vars'; open my $pipe_fh, '|-', 'sqlplus.exe -s uid/pwd@myserver' or die "Can't open pipe: $!"; print {$pipe_fh} <DATA>; close $pipe_fh or die "close failed: $!"; if ( $CHILD_ERROR ) { die "sqlplus returned non-zero exit status '$CHILD_ERROR'"; } __DATA__ select * from emp; exit;
Note that I haven't tested this, but I think it should run.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question on inline redirection
by jujiro_eb (Sexton) on Mar 17, 2009 at 21:01 UTC | |
by Anonymous Monk on Apr 28, 2012 at 22:14 UTC |