In a private mail exchange with a friend, talking about Perl, he wrote "aiutino" (long story, Italian monks will understand!) at which point I replied with

/ \b aiutino \b (?{ warn "Argh!" }) /gix;
Now I know that the extended regex (?{ code }) feature is still deemed experimental. But I really wanted to use it both for the sake of illustrating it and because IMHO it strongly stresses the idea that each use of the "incriminated" word is strictly associated with the emission of an "Argh!" warning.

To be sure, before sending the mail, I tried the code, and much to my surprise only one warning was emitted even if a line contained more instances of "aiutino".

So I tried

() = / \b aiutino \b (?{ warn "Argh!" }) /gix;
instead, and it did work as expected. Now it seems that the /g modifier on the match operator doesn't make it really global in scalar (or void) context, which could be because of an optimization effect, since for ordinary matches it doesn't really make a difference.

Indeed in quite a few years of Perl programming, I had never realized this was the case. But! if one deliberately specifies the /g modifier even in scalar context, then he should get what that it promises, and be them as experimental as you like, extended regex features executing code may be a good reason to guarantee it.

Any thoughts?


In reply to /g matches not really global in scalar context! by blazar

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.