Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Closure question

by Limbic~Region (Chancellor)
on Mar 02, 2004 at 18:05 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found