I don't know enough about printing setups to see if the information could be obtained elsewhere, so let's see if we can speed up the parsing.

I don't have any example files to see if my assumptions are sound, so I will try to make them explicit, and you can discard the advice that is rendered invalid by the incorrect ones.

First of all, I assume that the "global" data (duplex/simplex, pagesize, document title) only appears at the start of the file, and not somewhere in the middle.

So you could wrap those tests with:

unless ($pagestate && $pagesize && defined($title)) { # your tests }
That way you skip almost half the checks for most of the file.

That leaves checks for number of pages, number of copies (oops, maybe that fits into the first section: the ones that only appear at the start).

Since everything (except the QTY=(\d+) match for the number of copies) starts with a postscript comment, you can wrap all those tests with

if (/^%%/) { # your checks }
Thus skipping all those lines which aren't PS comments, and thus can't contain any data of interest to your script.

In the worst case, only the QTY=(\d+) check needs to look at all the lines of the file, and if that's a header notation, not even that.

Oh, and one more thing:Unless the last value for pages you find in the file is the applicable one, you might put

last if (defined($doctitle) && $copies && $pagesize && $pagestate);
With that, the code will stop reading the file as soon as it has all the data it needs.

In reply to Re: Fast file parsing by matija
in thread Fast file parsing by Anonymous Monk

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.