Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Recursive subroutines and closures (updated)

by AnomalousMonk (Archbishop)
on Sep 21, 2022 at 05:41 UTC ( [id://11147048]=note: print w/replies, xml ) Need Help??


in reply to Recursive subroutines and closures

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Recursive subroutines and closures (updated)
by ibm1620 (Hermit) on Sep 22, 2022 at 02:29 UTC
    Thanks for the link to Higher-Order Perl. Interested to learn more about closures....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found