Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 having a coffee break in the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found