APM: Asking again because I'm dense... Running a prog from a prog?

Travis hcoyote at ghostar.ath.cx
Fri Oct 20 18:02:54 PDT 2006


On Fri, Oct 20, 2006 at 03:08:12PM -0500, CaptNemo wrote:
> Sorry to ask this again but, I just can't grasp system() and get it to 
> work....
> 
>  I have written a program named gettables.pl which reads all the tables in 
> a database and prints them out.   The program is run with this line:
> 
>         my $command = "gettables.pl mysql database user password";
>         @dbtable = system($command);

You can't use system() that way.  It's used to just run a command.  Think
of it as shelling out of your program for a moment.  If you want to capture
the output, you need to use backticks, qx(), or open().

Completely untested, , but I'd do it this way:

    my $command = "gettables.pl mysql database user password";
    open(my $fh, "$command |");

    if (defined $fh) {
        while (my $output = <$fh>) {
            do_something($output);
        }
    } else {
        print_polite_web_failure();
    }

Or, if the output isn't large (or you need all the data before you can do
anything with it), you could just read it all into an array like:

    if (defined $fh) {
        @data = <$fh>;

        print_table(@data);
    } else {
        print_polite_web_failure();
    }


Travis
-- 
Travis Campbell
hcoyote at ghostar.ath.cx


More information about the Austin mailing list