I've been struggling with this issue for a while, and basically I'm trying to output a lot of data from Perl to a less screen so that users can scroll through it. Before that even takes place I'm doing an SQL query and returning 28 fields, which I then load into an array so that I can split and parse these fields into individual variables. Some of them contain newlines which is why I do the split/splice thing. While I'd like to do this in memory for maximum performance, sometimes the output can be up to a million lines or more, and this uses a ridiculous amount of memory the way I'm doing it. Currently I'm using this method to query a DB and later output from Perl:
my @results = split(/dbDelimeter/,`SQL_Query`); while(@results) { (var1,var2,var3,etc) = splice(@results, 0, 28); $var1 =~ s/\n//g; $var2 =~ s/oldDateFormat/newDateFormat/; $var3 =~ s/moreReplacements/DoneHere/; $output = sprintf (%-10s %-10s %10s,$var1,$var2,$var3); $finalOutput .= $output; } open LESS, '|less -cSRM' or die $!; print LESS $finalOutput; close LESS;
I wanted to try using a temp file to be safe and not blow up the memory on the server. Then I ran into even more issues. I tried using an "anonymous file handle" because I thought it would be nice to have Perl handle the cleanup. Couldn't get that to work:
open(my $data, "+>", undef) or die; $data = `SQL_Query`; ## Method 1 print $data `SQL_Query`; ## Method 2 ## processing to parse and format data system("less $data"); ## Output: None / Doesn't Work print $data; ## Output: GLOB(0x1c8ef430)
It would be great if somebody could show me different, efficient ways to output data to a less screen, both using memory and the disk if possible. Essentially I want to store all this data on the disk because of the excessive memory usage (unless I'm doing something wrong to use so much memory..) Thanks!

In reply to Print to Less Screen by bigbot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.