[Melbourne-pm] Closures and scope warnings

Scott Penrose scottp at dd.com.au
Tue Jul 27 20:58:35 PDT 2010



----- Original Message -----
> Toby Corkindale wrote:
> > Hmm, here's something that caught me out.. probably because it's not
> > great practice. (But it is a common theme in using DBIx::Class
> > transactions)
> >
> > my $thing;
> > sub { my $thing = 'wibble' }->();
> 
> This isn't just constrained to this circumstance, and is probably
> normally
> considered a feature. Consider:
> 
> my $name = "Jacinta";
> 
> print_name($name);
> 
> #### much later
> 
> sub name {
> my $name = shift;
> 
> print "$name\n";
> return; }
> 
> 
> Would you *really* want every subroutine which creates variables which
> shadow top-level variables to warn about such? I don't think Perl
> distinguishes between your case and mine.

I agree with Toby, it should.

If I do:

my $name = "Blah";
while (<>) {
  my $name = $_;
  print $name . "\n";
}

It produces a warning, why is a sub block treated differently to a while loop. I know that it is, but from a warning perspective, if I overload a "global" (in Jacinta's example) variable with one in a sub, then yes, it should warn.

Scott

-- 
http://scott.dd.com.au/
scottp at dd.com.au




More information about the Melbourne-pm mailing list