Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Closure Over Scalar?

by LanX (Saint)
on Feb 18, 2021 at 16:03 UTC ( [id://11128526]=note: print w/replies, xml ) Need Help??


in reply to Closure Over Scalar?

you initialize my $FIXED_STRING = 'fixed_string'; only AFTER calling the sub.

Not really a closure problem, could happen with package vars too.

One best practice to avoid such problems is to use a "generator sub" with the whole closure-context like in your block and returning \&do_something

update

like:

sub generate { my $FIXED_STRING = 'fixed_string'; my %persistent; return sub { ... }

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2025-06-23 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.