OTOH, consider (ta-daaa) Lexical Subroutines:
Win8 Strawberry 5.30.3.1 (64) Wed 09/21/2022 1:32:21
C:\@Work\Perl\monks
>perl
use 5.018; # lexical subroutines
use strict;
use warnings;
Outer(1);
# Inner(2); # Undefined subroutine &main::Inner ...
sub Outer {
my ($param) = @_;
my sub Inner {
my ($innerVar) = @_;
print "In inner: $innerVar\n";
}
Inner($param);
}
^Z
In inner: 1
I haven't used lexical subs very much myself, so I can't offer advice about possible pitfalls. And
closures are always a bit tricky.
If you really want to have fun with closures, see Dominus's Higher-Order Perl (free download).
Update: And the second of GrandFather's examples:
Win8 Strawberry 5.30.3.1 (64) Wed 09/21/2022 1:46:46
C:\@Work\Perl\monks
>perl
use 5.018; # lexical subs
use strict;
use warnings;
Outer(1);
# Inner(); # still not there
Outer(1);
sub Outer {
my ($param) = @_;
my sub Inner {
print "In inner: $param\n";
++$param;
}
Inner();
}
^Z
In inner: 1
In inner: 1
Give a man a fish: <%-{-{-{-<