Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: simple anonymous subroutine and variable interpolation timing question

by ikegami (Patriarch)
on Jun 23, 2011 at 21:24 UTC ( [id://911173]=note: print w/replies, xml ) Need Help??


in reply to Re^2: simple anonymous subroutine and variable interpolation timing question
in thread simple anonymous subroutine and variable interpolation timing question

You don't have to create a separate sub. Alternatives:

my $sub = sub { my ($str) = @_; sub { $str } }->($str);
my $sub = sub { my $str = $str; sub { $str } }->();
my $sub = do { my $str = $str; sub { $str } };
my $str_copy = $str; my $sub = sub { $str_copy };

In practice, it's usually a non-issue since you either have a variable that doesn't change to begin with

for my $str (...) { ... sub { ... $str ... } ... }
or you want to factor out the code to a sub anyway.
sub make_list_iterator { my @list = @_; return sub { return @list ? shift(@list) : (); }; } my $iter = make_list_iterator(@list);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2025-05-18 01:32 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.