This seems very strange to me, but it works:
prints#!/usr/bin/perl use strict; use warnings; my $n1 = 10; our $n2 = 20; sub foo { $n1++; # ??? $n2++; } print "$n1\n"; print "$n2\n"; foo; print "$n1\n"; print "$n2\n";
10 20 11 21
I would expect the $n2 package global to work fine in the above code, and it does. But how does perl allow foo() get away with referring to $n1 like that? When parsing sub foo, shouldn't perl say, "Nope. There's no local here named $n1, and no package global named $n1, so I don't see which $n1 you're talking about."?
BTW, I can see just fine how the following works:
because that's just how scoping works. But it looks to me like there's supposed to be a distinction between that and the way foo() above is doing business... (confused)my $bar = 2; { print "Hi \$bar ($bar), good to see you.\n"; }
In reply to Using an "outer" lexical in a sub? by cornballer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |