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

Does anyone know if there is a pager (like 'less' or 'more') written in perl?

I'd like to add customizable syntax highlighting to a pager like 'less'.

I already know about LESSOPEN="| prog %s" and less.vim and ccze. I want to apply custom syntax highlighting to large log files with all of the capabilities of 'less', i.e. up/down line/page, forward and backward regex search, go to line number, go to absolute position offset (+NNNNP), tail mode (+F), etc.

Any ideas? Naturally I'd prefer to write the syntax highlighting logic in perl, but that's not an absolute requirement.

Replies are listed 'Best First'.
Re: pager written in perl?
by MidLifeXis (Monsignor) on Dec 01, 2011 at 19:02 UTC
      Yes, and I already know about that.

      The main problem is that, AFAICT, less doesn't support random access of the file when using a LESSOPEN filter.

      The log files I want to colorize are very large, and it is impractical to colorize the entire file. Also, I want search to be performed on the raw contents, not the colorized version.

Re: pager written in perl?
by Sewi (Friar) on Dec 01, 2011 at 21:07 UTC

    Writing a pager isn't that hard.

    You might want to use sys* commands to avoid Perl buffering:

  • sysopen your file
  • sysseek to the position
  • Read a fixed length block and look for a line ending
  • Show a line if you find a break, read another block if there is no line ending left
  • Repeat the last two steps until as much lines are shown as you like
  • You may want to save the positions of the last line beginnings using systell to directly sysseek to lines above and below the current screen.

    Writing a complete less-like pager may be require more features, but a basic one may also do the job if you're only viewing logfiles.

    I try to use the head and tail Linux commands to limit the number of lines when working with huge logfiles, for example "tail -n 25000 httpd/access.log" to get a day on a medium busy webserver. grep is also good if you're searching for specific information, less and it's coloring will do a good job when viewing the results.