|
|
| No such thing as a small change | |
| PerlMonks |
Re: foreach-loop-local var in subby tobyink (Prior) |
| on Jan 21, 2013 at 16:06 UTC ( #1014478=note: print w/ replies, xml ) | Need Help?? |
|
sub definitions within control structures don't really work how you want them to work. Your my_print function only gets defined once; not each time around the loop. If you want to redefine the sub each time around the loop, use an anonymous sub (a.k.a. closure; coderef) - which can be given a name by assigning it to a glob. This works:
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||