Actually, the output is:
Well, it would be if you would use strict. This would also do a lot to help you understand the closure you're using here. The $i inside the outter braces is a lexical that goes out of scope - while the $i that you have outside the braces is the global $i. You're probably confusing them with being the same variable.Global symbol "$i" requires explicit package name at ./z.pl line 13. Execution of ./z.pl aborted due to compilation errors.
Try some more tests - e.g., move the $i=10 line inside the braces, and suddenly you'll get 13. That's because you're now affecting the same lexical $i that addone is using. Hopefully that will help cement the idea :-)
Note that not all anonymous subs are closures. That's just one really great use of anonymous subs. For example:
This is an anonymous sub, but it isn't a closure - it isn't "closed" on any lexical variable. (PS - don't run that blindly ;->)use File::Find; find(sub { rmdir $_ || unlink $_ }, @ARGV);
In reply to Re: Nailing down closures!
by Tanktalus
in thread Nailing down closures!
by mattford63
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |