At 03:03 PM 8/5/00 -0400, Mark-Jason Dominus wrote:
>3. Variables should be lexical by default:
>
>    a. Perl should be conservative about inference and issue a warning
>       when scopes appear to overlap, as in the example above.  In such
>       cases a declaration at the outer scope may be required for
>       correct behavior.
>
>    b. Perl should be more liberal about inference and should try to do
>       the right thing without warnings, as in the example above.
>
>    This is my position (although I haven't committed to 3a or 3b yet
>    because the details are not explicit), and it's *also* consistent
>    with what you say above, so I'm not sure that you're even
>    disagreeing with me.
>[snip]
>Example 3:
>
>     YOU WRITE:                     PERL INFERS THAT YOU MEANT:
>
>                                    my $switch;
>     if (condition) {               if (condition) {
>        $switch = 1;                  my $switch = 1;
>     }                              }
>     # ...                          # ...
>     print "..." if $switch;        print "..." if $switch;
>
>                                    WARNING: $switch on line 2 is not
>                                             the same as $switch on line 5
>
>
>Here's the one with the problem.  Perl infers too many declarations
>and you don't get what you want.  But you do get a warning.  To fix
>it, you add the declaration to make it look like example 1.  This is
>not obfuscated or confusing because the resulting code has exactly the
>same meaning in Perl 5 and in Perl 6.
>
>The rule for how Perl infers 'my' is:  Perl infers a 'my' at the
>innermost possible scope for every variable that isn't explicitly
>declared in some outer scope.

What says that Perl must infer the declaration of the outer $switch before 
the inner block, rather than right before its first use:

if (condition) {
   my $switch = 1;
}
# ...
my $switch;
print "..." if $switch;

Don't get me wrong, I agree with the way you had it (inferred declarations 
occur as early as possible in the inferred scope).  But it seems that it 
could be either way and I would like to know whether there is any advantage 
to doing it the other way.

Doing it the other way would appear to make the warning impossible, which 
is certainly a disadvantage.


--
Peter Scott
Pacific Systems Design Technologies

Reply via email to