[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [ID 20000122.001] Incorrect scoping of if(){}else{}



Bart Schuller writes:
: On Sat, Jan 22, 2000 at 02:25:00PM +0000, M.J.T. Guy wrote:
: > 	if (my $x = 1, 0) {
: > 	    my $x = 2;
: > 	} else {
: > 	    my $x = 3;
: > 	}
: > 
: > produces spurious warnings
: > 
: > "my" variable $x masks earlier declaration in same scope at - line 2.
: > "my" variable $x masks earlier declaration in same scope at - line 4.
: 
: The only reason why I would use if (my $x = 1, 0) is precisely to get it
: scoped like perl seems to do here. That is: I expect to be able to see
: $x in both the "if" and the "else" branch, but not outside of them.

I think the point of it is that the first declaration is within an
implicit block around everything, and therefore the inner blocks are
not in fact the "same scope", so the warnings are spurious.  It should
be equivalent to saying:

    { my $x;
	if ($x = 1, 0) {
	    my $x = 2;
	} else {
	    my $x = 3;
	}
    }

and it isn't.

That being said, I'm not sure we should remove the warning, since the
outer block is implicit, and the presence of such a redeclaration is in
all likelihood an error.  I imagine it was an error that caused the
problem to be noticed in the first place, no?

But the message is still slightly false.

Larry


Follow-Ups from:
Gurusamy Sarathy <gsar@ActiveState.com>
References to:
Bart Schuller <schuller@lunatech.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]