hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!

I'm using DBD::mysql to work with my mysql server and I have a script that queries on the DB and prints the result to screen, something like:
# connect to db # prepare query $query->execute(); while ($row = $query->fetch_hashref()) { print "ID: $row->{ID}, $row->{MSG_TEXT} ..... \n"; }
The problem is that my query usually returns a lot of rows (1000 or so), and the output is printed to screen without possability to track it. So I tried printing the result to a temporary file and less on the file (Linux less - like that I don't need to implement a pager), but if the output is very long it takes time to build the file and the user who runs the script waits until less kick into gear.
Is there in Perl a way I can start immediately displaying the output to console and still have paging capabilities?

Replies are listed 'Best First'.
Re: Mysql and printing result
by Aragorn (Curate) on Oct 20, 2003 at 13:45 UTC
    Maybe this will help you on your way:
    use strict; use warnings; # Save standard output handle open(OLDOUT, ">&STDOUT"); # Reopen standard output as a pipe to the 'less' program open(STDOUT, "| /usr/bin/less") or die "$!\n"; # Output some stuff for (1..128) { print "Line: $_\n"; } # Close the modified STDOUT and reopen it as the original STDOUT again close(STDOUT); open(STDOUT, ">&OLDOUT") or die "Can't restore STDOUT: $!";


Re: Mysql and printing result
by theorbtwo (Prior) on Oct 20, 2003 at 10:30 UTC

    Yes, there is a perl way to do it. In fact, I can think of at least two. However, there is also a non-perl WTDI that's a lot easier. perl|less. (BTW, that should work on both windows and unix, though windows has strange corner cases where it won't work -- with .bat files, and associations, IIRC.)

    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Thanks but I need it from within perl, the code for printing the result is actually a function that is called from another perl script and not a script by itself.
      It's a part of a command line shell I wrote, and when a user enters the command lessOnDB the function is called and the output should be displayed. So I need a stict perl way for doing that.
Re: Mysql and printing result
by schumi (Hermit) on Oct 20, 2003 at 13:38 UTC
    Why don't you print the output to several smaller files? Depending on what you need the data for and wat you're going to with it, this would shorten the time until the user gets some output, and you might even consolidate the files again for later use.

    Note that this is just off the top of my hat, not really thought through and maybe completely useless.


    There are nights when the wolves are silent and only the moon howls. - George Carlin