G'day songmaster,

As LanX alluded to in his update, I suspect the /o modifier may be the issue: it certainly stood out as I read through the code fragments you provided.

In "perlre: Modifiers" you'll see:

"o  - pretend to optimize your code, but actually introduce bugs"

That provides a link to further information in "perlop: Regexp Quote-Like Operators" but the fragment identifier (#s%2fPATTERN%2fREPLACEMENT%2fmsixpodualngcer) is wrong. The closest to that is probably #s/_PATTERN_/_REPLACEMENT_/msixpodualngcer; however, the one with the most information about /o, and probably more appropriate given the code you've shown, is #m/_PATTERN_/msixpodualngc, which culminates in:

"The bottom line is that using /o is almost never a good idea."

I probably would have created all of those regexes at compile time, and I would have used my instead of our variables. A dispatch table with actions based on matches may also be appropriate.

You don't show sufficient code to make any direct modification recommendations. The following script simply suggests a technique you could adapt to your needs.

#!/usr/bin/env perl use strict; use warnings; my %capture; BEGIN { my $RXone = qr{(?x: 1 )}; my $RXthree = qr{(?x: 3 )}; my $RXnum = qr{(?x: $RXone | $RXthree )}; my $RXstr = qr{(?x: ( [a-z]+ $RXnum ) )}; %capture = ( menu => { regexp => qr{(?x: ^ menu \s+ $RXstr $ )}, action => sub { parse_menu(@_) }, }, driver => { regexp => qr{(?x: ^ driver \s+ $RXstr $ )}, action => sub { parse_driver(@_) }, }, ); } my @capture_keys = keys %capture; while (<DATA>) { for my $capture_key (@capture_keys) { if (/$capture{$capture_key}{regexp}/) { $capture{$capture_key}{action}->($1); last; } } } sub parse_menu { print "MENU: @_\n" } sub parse_driver { print "DRIVER: @_\n" } __DATA__ menu menu1 driver driver1 other other1 menu menu2 driver driver2 other other2 menu menu3 driver driver3 other other3

You may have sufficient, up-front knowledge about those "capture keys" to predefine an ordered @capture_keys rather than relying on the random list returned by keys.

Output from a sample run of that script:

MENU: menu1 DRIVER: driver1 MENU: menu3 DRIVER: driver3

Update (minor code alteration): My original code had $capure_key (missing "t") throughout. I've changed that to $capture_key globally; retested; output unchanged.

— Ken


In reply to Re: Parser Performance Question by kcott
in thread Parser Performance Question by songmaster

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.