in reply to strange scope

Thanks for all the feedback folks.

Zaxo addressed the issue concisely - thanks.

For the other posts that ranted about the logic, I am a bit surprised - first, because it's a bit odd to change the logic of some code to fix a warning, and second, it ignores TMTOWTDI (although the trend in paper tomes on Perl seem to be straying from that mantra).

Anyways, here's the actual code snippet (parsing S-Expr subset):

my $text; if (($text) = $line =~ m!^\s*\((.*)\)[\r\n]+$!) { # keyed value my ($key, $val) = split( /\s+/, $text, 2 ); $val =~ s!(^[\'\"]|[\'\"]$)!!g; $info->{$key} = $val; } elsif (($text) = $line =~ m!^\s*\'(.*)\'[\r\n]+$!) { # quoted value $text =~ s!(^[\'\"]|[\'\"]$)!!g; return $text; } elsif (my ($key) = $line =~ m!^\s*\((\S+)[\r\n]+$!) { # sub-level push @{$info->{$key} ||= []}, $self->_parse_vm_info( $lines ); }

And a solution to the warning was never very interesting to me - I had solved it by polluting the enclosing block (as above) and while I liked the single initial def by Zaxo, I chose not to use that because future edits could make the def inadvertently disappear for the 2nd block (a minor annoyance).

Onward, McPerl

Replies are listed 'Best First'.
Re^2: strange scope
by naikonta (Curate) on May 25, 2007 at 10:30 UTC
    first, because it's a bit odd to change the logic of some code to fix a warning
    I don't think that logic changing is solely for fixing the warnings. The warnings show up because you ask for it, you ask Perl to warn you if it thinks that you might do something you don't intent. Perl is not always right about this, but mostly is. And there's another way to supress the warning if you're sure about what you're doing without changing the logic. Even if the changing is solely for fixing the warning, I don't see it as odd at all. Most Perlers, at least those around here, don't like the warnings show up, and clutter the error log unintentionally (in the case of web applications).
    second, it ignores TMTOWTDI
    How the logic changing ignores TMTOWTDI? Changing the way something is done is clearly to show another way to do it.
    And a solution to the warning was never very interesting to me
    I think I understand your intent in your OP. You think the elsif is in the equal scope as if but Perl doesn't think so hence the warning. But you're interested in knowing why instead of how to get rid of the warnings. The thing is, if you try to understand what the warnings mean, you'll know how to solve the real problem, and get rid of the warnings as well. Hopefully.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!