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?)
The output goes as follows: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;
$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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |