Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Mysql and printing result

by hotshot (Prior)
on Oct 20, 2003 at 10:17 UTC ( #300517=perlquestion: print w/replies, xml ) Need Help??
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://300517]
Approved by castaway
[Eily]: marto you just need a longer cable :P
[Corion]: marto: Yeah, but I'm somewhat wary of installing random USB drivers downloaded from , Google Drive or whatever, so Linux wins there due to there being no conflicts and me just having to edit one text file in the worst case, to add the USB vendor
[hippo]: Long USB cable FTW.
[Corion]: Eily: I've thought of that, but I don't like running long cables through the appartment because sooner or later I'll trip over it, pulling at least one device off its stand :)
[marto]: hmm, may have to patch CPAN::Meta to move from search.cpan to metacpan in the META.json/yml files
[Corion]: marto: Heh - it seems that they plan to keep the links alive for a long time. But still, I plan on moving PM to use/generate the new links
[Corion]: And I think it's better to generate links to the new world instead of keeping the older links alive by generating new versions of them ;)
[marto]: yeah, I guess it's supposed to be a permanent redirect, but better to make the move where possible

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2018-05-23 09:18 GMT
Find Nodes?
    Voting Booth?