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: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|