I wish to tokenize a stream and I'm interested in understanding the qr// operator a bit better, in particular how one uses the /g modifier with it. I'm not so much interested in pointers to CPAN for modules to do this for me, though I would appreciate pointers on how tokenizing is done if I am going about it in a stupid way.

Specifically, I want to read in a list of possible tokens, build a pattern on the fly to recognize them, and then rip through an input string iteratively, reporting the tokens found within it. I figured that the way to do this was to concatenate all the possible token descriptors with the regex alternation symbol (|), embed the whole thing in parentheses to do capture, get a regex for it using the qr// operator with the /g modifier, and then have a while loop in whose conditional clause I bind the regex to the token stream. So, pseudo-code something like this...

my $stream = 't1 t2 t1 t3 t1 t4'; my @tokens = ('t1, 't2', 't3', 't4'); my $pattern = join('|', @tokens); my $regex = qr/($pattern)/g; while ($stream =~ $regex) { print "token: $1\n"; }

Alas, this seems not to work. Perl gripes "bareword found where operator expected" where I try to put the /g modifier. I wondered if I had botched the syntax, so I tried the /i modifier, and that works, so no, apparently it just doesn't like having the /g modifier stuck on the qr// operator. Hm... So, I changed the while loop to this...

while ($stream =~ /$regex/g) { print "token: $1\n"; }

That works just fine, but I'm left wondering what exactly Perl is doing when I write my code thusly. Namely, I'm wondering if it is any better than doing this...

while ($stream =~ /($pattern)/g) { print "token: $1\n"; }

I want to build this tokenizer pattern only once, and I'm concerned that the ways I've hacked around qr//'s apparent refusal to allow a /g modifier doesn't accomplish this. Namely, I'm worrying that the pattern is getting recompiled every time I use it since I'm embedding it in a new //.

So, what's the deal? Why won't qr// take /g? Is this a bug, a feature, or just a complete lack of comprehension on my part? I'm rather confused.


In reply to Tokenizing and qr// <=> /g interplay by skyknight

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.