hi all,

I'm looking for an explanation. I've crafted some code, it does what I want (well, that's what i think)... but now I want to know why ;-)

I have a if statement with a call to a method in a regular expression. I use the ??{ } construction in the regular expression in order to execute the method. (maybe there's a better way?)

package MyClass; my $cnt; sub new { return bless { cnt => 0 }; } sub gimme_sweet_love { my $self = @_; $self->{cnt} += 1; return "hell yeah" } sub cnt { my $self = @_; return $self->{cnt} } package main; use strict; use warnings; local $\ = $/; my $match = "hell yeah"; my $obj = MyClass->new(); my $re = qr/(??{ ${obj}->gimme_sweet_love })/; print "\$match value:", $match; print "HURAY (m)" if $match =~ m/$re/; print "count = ".$obj->cnt; $match = "hell no"; print "\$match value:", $match; print "HURAY (m)" if $match =~ m/$re/; print "count = ".$obj->cnt;
The output goes as follows:
$match value:hell yeah HURAY (m) count = 1 $match value:hell no count = 9

So, when there's no match, the above method $obj->gimme_sweet_love is executed "the number of chars" times, could someone tell me why?

when there's a match, the method is executed only once.

Thanks a lot

to ask a question is a moment of shame
to remain ignorant is a lifelong shame


In reply to ??{ } oddity by insaniac

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.