[Melbourne-pm] Closures and scope warnings

Hamish Carpenter hamish at hamishcarpenter.com
Tue Jul 27 23:01:00 PDT 2010


I know its not compile time, but perlcritic can warn on these cases.
See http://search.cpan.org/perldoc?Perl::Critic::Policy::Variables::ProhibitReusedNames

Using Damian's exmaple of:
   my $name = "Jacinta";

   {
       my $name = 'shift';

       print "$name\n";
   }

Results in:

Reused variable name in lexical scope: $name at line 6, column 8.
Invent unique variable names.  (Severity: 3)

On Wed, Jul 28, 2010 at 2:40 PM, Alfie John <alfiejohn at gmail.com> wrote:
> On Wed, Jul 28, 2010 at 2:19 PM, Damian Conway <damian at conway.org> wrote:
>>
>> Scott asked:
>>
>> > So... the rule is that warnings only happen within the same level of
>> > block?
>>
>> Yes. The second declaration of a variable name at a given block level
>> generates
>> a warning. Two declarations in different blocks never has (and never
>> will).
>
> It's funny because 'my' will warn, but 'local' is fine:
>
>   perl -we 'use strict;our $f = 10; {my $f = 20; my $f = 30; print $f,"\n"}
> print $f,"\n"';
>   perl -we 'use strict;our $f = 10; {local $f = 20; local $f = 30; print
> $f,"\n"} print $f,"\n"';
>
> The fact that 'local' doesn't warn makes it seem inconsistent to me. But it
> could be just the way how I think 'local' works ;p
>
> Alfie
>
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>


More information about the Melbourne-pm mailing list