As ikegami said, subs are global:
#!perl use v5.10; use strict; use warnings; my $a = shift; sub f { say "global f"; } sub g { say "global g"; my ($a) = @_; sub f { say "local f ($a)"; # line 16 } f(); } f(); g(shift); f(); __END__ C:\Temp>localsub.pl Variable "$a" will not stay shared at C:\Temp\localsub.pl line 16. Subroutine f redefined at C:\Temp\localsub.pl line 15. Use of uninitialized value $a in concatenation (.) or string at C:\Tem +p\localsub.pl line 16. local f () global g Use of uninitialized value $a in concatenation (.) or string at C:\Tem +p\localsub.pl line 16. local f () Use of uninitialized value $a in concatenation (.) or string at C:\Tem +p\localsub.pl line 16. local f () C:\Temp>localsub.pl AAA BBB Variable "$a" will not stay shared at C:\Temp\localsub.pl line 16. Subroutine f redefined at C:\Temp\localsub.pl line 15. Use of uninitialized value $a in concatenation (.) or string at C:\Tem +p\localsub.pl line 16. local f () global g local f (BBB) local f (BBB)
What is a strange behavior is that variables got stuck with a value, even if it is assigned after it's first use... That does not seem to be a compile time assignment.
Conclusion: always define global subroutines or pass variables as parameters to them if defined locally for clarity.
In reply to Re: closure clarity, please
by vitoco
in thread closure clarity, please
by 7stud
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |