maard has asked for the wisdom of the Perl Monks concerning the following question:
While extending functionality of one of the CPAN modules I've run into something strange.
The code is:
I was amazed to see that on each iteration $namedisplay is being assigned the same value (the $obj->{labels}->{$fields->[0]} one).foreach my $name (@$fields) { # ... my $namedisplay = $1 if($name =~ /AS\s+(\S+)/i); warn "namedisplay: $namedisplay"; # 1-st warn # that's the lines I added to the code: if ( !$namedisplay && exists($obj->{labels}->{$name}) && $obj->{labels}->{$name}) { $namedisplay = $obj->{labels}->{$name}; } warn "namedisplay: $namedisplay"; # 2-nd warn # ... }
After splitting the line where $namedisplay first occured into declaration and assignment parts everything became OK.
my $namedisplay; $namedisplay = $1 if($name =~ /AS\s+(\S+)/i);
But I still don't understand what was happening before fix. I understand that $namedisplay was declared regardles of regex match (I was even begining to think the entire declaration could be skipped if regex didn't match). But it somehow remained alive throghout all loop iterations (1-st warn showed the same value for the variable on all iterations exept first one).
Will wise monks bring some light to this problem?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: will you explain what's going on?
by fglock (Vicar) on Oct 26, 2004 at 18:06 UTC | |
by chromatic (Archbishop) on Oct 26, 2004 at 19:26 UTC | |
by dave_the_m (Monsignor) on Oct 26, 2004 at 23:52 UTC | |
by maard (Pilgrim) on Oct 27, 2004 at 10:29 UTC | |
by ysth (Canon) on Oct 27, 2004 at 12:06 UTC | |
by fglock (Vicar) on Oct 26, 2004 at 23:17 UTC | |
by maard (Pilgrim) on Oct 27, 2004 at 10:23 UTC | |
by ysth (Canon) on Oct 27, 2004 at 11:58 UTC |