I think what's not being stated explicitly is that in your first example, you've created an anonymous subroutine and then returned a reference to it. Executing the coderef triggers the
code, despite the fact that it's in a different lexical context than the
coderef. You passed in the value to count_maker(), and the value was passed to the anonymous sub because that sub is in the same lexical context as the $count you shifted in.
In your second example, my $count is in the lexical context of the do{} loop. You don't have strict turned on, so you can access counter(), but the my $count there overrides your declaration of $count in the for{} loop.
Closures are subtle, but in the context of what you're trying to do, you should be returning a coderef for it to work. Once you get that, read some of the closure articles again.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.