insaniac has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ??{ } oddity
by Joost (Canon) on Apr 28, 2005 at 12:35 UTC | |
by insaniac (Friar) on Apr 28, 2005 at 12:41 UTC | |
by Roy Johnson (Monsignor) on Apr 28, 2005 at 15:54 UTC | |
by insaniac (Friar) on Apr 28, 2005 at 17:09 UTC | |
|
Re: ??{ } oddity
by Roy Johnson (Monsignor) on Apr 28, 2005 at 17:09 UTC | |
by insaniac (Friar) on Apr 28, 2005 at 17:13 UTC |