My dearest brethren, I pray thee will help me on my quest for enlightenment.

I am, currently, building a large project, part of which involves a complicated parsing procedure, for which I am hoping to enlist the help of perl's regex engine. However, it is somewhat limited in the area I need help in. Take the following example regex: /(?:(\d)\w))+\d\w/ Not too complicated, but it will be, because if you know your regexes, you know that $1 will only contain the value of the last match. Meaning matching that regex against "1b2b3b4b5b" will result in $1 being equal to 4. There is a way to get to the other values, however. As japhy proposes in his book, the code:

my @vars; $string =~ /(?{local @1 = ();} (?:( (\d) (?{ local @1 = (@1, $1) }) \w )+ \d\w (?{ @vars = @1 /x
will match and stuff the vars in @vars, though in a somwhat roundabout way. My need goes even further, however, as I need to create these dynamically. I can, and have, done this, with one problem. In order to speed this up, I have tried to use qr//. I am doing this in an OO interface, so that it works well with the rest of my program. But qr// doesn't seem to like the above regex. It doesn't complain, but it somehow messes up other variables in my program. Objects that are in a different module altogether. I don't know how or why, but it is, even though I use warnings, strict, and what I think are good coding practices.

My question is, why does it do this? When I take the local command out of the regex, everything works, and my other variables aren't messed up. In order to get the speed and interface I want, I need to figure out how to do this. Thank you,

elusion : http://matt.diephouse.com


In reply to Using qr// with a complicated regex by elusion

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.