If you want $sub
to be a variable that is forcably
undefined implicitly at the end of a scope scope then Perl has just
such a thing - the local()
variable!
It is an annoying miss-feature that local()
can't be applied to
lexical variables so if you want to use local()
you are for forced to
use package variables. What one really wants to do is...
# Won't work my $sub; local $sub = sub{ blah; $sub->() }
...but one is forced to do...
our $sub; local $sub = sub{ blah; $sub->() }