Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 learning in the Monastery: (4)
As of 2024-06-18 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.