Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Closure question

by Limbic~Region (Chancellor)
on Mar 02, 2004 at 18:05 UTC ( #333324=note: print w/replies, xml ) Need Help??


in reply to Closure question

Pug,
What you are using is a one-use closure. Because it is in a naked block it can not be used multiple times. And as broquaint points out, it must be "seen" prior to being used. Here is an example of a re-usable closure that can be used before or after the sub has been "seen".
#!/usr/bin/perl use strict; use warnings; my $count_1 = iterator( 1 ); print $count_1->(), "\n" for 1 .. 10; my $count_2 = iterator( 'a' ); print $count_2->(), "\n" for 1 .. 10; sub iterator { my $iterator = shift; return sub { $iterator++ }; }
Cheers - L~R

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2021-11-28 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?