Open the data transfer process (sqlplus) in perl and get the information from the query?

Basically, I would like to openconnect to sqlplus using Perl by sending a query and then returning the information from the query.

Current Code:

open(PIPE, '-|', "sqlplus user/password@server_details");

while (<PIPE>) {
    print $_;
}

This allows me to go into sqlplus and execute my query.

I'm having trouble figuring out how to pass a sqlplus query to Perl (since it's always the same query), and once that is done, how can I get the information written back to the variable in my Perl script?

PS - I know about DBI... but I would like to know how to do this using the above method, as inelegant as it is :)


, sqlplus, ... , .

my $squery = "select column from table where rownum <= 10;"

# Open pipe to sqlplus, connect to server...
open(PIPE, '|-', "sqlplus user/password@server_details") or die "I cannot fork: $!";
# Print the query to PIPE?
print PIPE $squery;

STDOUT sqlplus, Perl (parent) script?

, .

:

Perl script (parent) → open pipe sqlplus () → → sqlplus (STDOUT?) → STDOUT Perl script ()


. , sqlplus , DBI. , - ...

+3
3

, Perl .

+5

, , , IPC:: Run. start, :

 my $h = start \@cat, \$in, \$out;

$input , $output.

$in = "first input\n";

## Now do I/O.  start() does no I/O.
pump $h while length $in;  ## Wait for all input to go

## Now do some more I/O.
$in = "second input\n";
pump $h until $out =~ /second input/;

## Clean up
finish $h or die "cat returned $?";

CPAN, , .

+3

If your request is static, move it to your own file and load sqlplusand execute it.

open(my $pipe, '-|', 'sqlplus', 'user/password@server_details', '@/path/to/sql-lib/your-query.sql', 'query_param_1', 'query_param_2') or die $!;

while (<$pipe>) {
    print $_;
}
0
source

Source: https://habr.com/ru/post/1789754/


All Articles