On Jan 6, 2010, at 6:31 PM, Kevin Grittner wrote:

> As far as I've been able to determine so far, to call psql in a
> relatively portable way would require something like this:
> 
> http://perldoc.perl.org/perlfork.html

Here's an example using IPC::Open3:

    #!/usr/local/bin/perl -w

    use strict;
    use warnings;

    use IPC::Open3;
    use Symbol 'gensym';
    use constant EOC => "__DONE__\n";

    my ($in1, $out1, $err1) = (gensym, gensym, gensym);
    my ($in2, $out2, $err2) = (gensym, gensym, gensym);

    my $pid1 = open3 $in1, $out1, $err1, 'bash';
    my $pid2 = open3 $in2, $out2, $err2, 'bash';

    print $in1 "cd ~/dev/postgresql\n";
    print $in1 "ls doc\n";
    print $in1 "echo ", EOC;
    while ((my $line = <$out1>)) {
        last if $line eq EOC;
        print "LS:   $line";
    }

    print "#### Finished file listing\n\n";

    print $in2 "cd ~/dev/postgresql\n";
    print $in2 "head -4 README\n";
    print $in2 "echo ", EOC;
    while (defined( my $line = <$out2> )) {
        last if $line eq EOC;
        print "HEAD:  $line";
    }

    print "#### Finished reading README\n\n";

    print $in1 "exit\n";
    print $in2 "exit\n";
    waitpid $pid2, 0;

    print "#### All done!\n";

With that, I get:

    LS:   KNOWN_BUGS
    LS:   MISSING_FEATURES
    LS:   Makefile
    LS:   README.mb.big5
    LS:   README.mb.jp
    LS:   TODO
    LS:   bug.template
    LS:   src
    #### Finished file listing

    HEAD:  PostgreSQL Database Management System
    HEAD:  =====================================
    HEAD:    
    HEAD:  This directory contains the source code distribution of the 
PostgreSQL
    #### Finished reading README

    #### All done!

I could easily write a very simple module to abstract all that stuff for you, 
then you could just do something like:

    my $psql1 = Shell::Pipe->new(qw(psql -U postgres));
    my $psql2 = Shell::Pipe->new(qw(psql -U postgres));

    $psql1->print('SELECT * from pg_class');
    while (my $line = $psql1->readln) { print "Output: $line\n" }
    $psql1->close;

All I'd need is some more reliable way than "echo "DONE__\n" to be able to tell 
when a particular command has finished outputting.

Thoughts?

Best,

David

   




-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to