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?
In reply to will you explain what's going on? by maard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |