Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Syntactic Confectionery Delight
 
PerlMonks  

Re: Your favorite objects NOT of the hashref phylum

by nferraz (Monk)
on Mar 25, 2006 at 16:49 UTC ( [id://539233]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Your favorite objects NOT of the hashref phylum

I don't know if it's the kind of object you're looking for, but I'm very interested in interators:

my $iterator = &foo(); while($iterator->()) { # do something }

The cool part is the way they can be created in Perl:

sub foo { return sub { # some code }; }

Now, you may be asking what's an iterator used for? Iterators can be used instead of arrays when the list in its entirety would use too much memory, or when the list is infinite.

For example, the set of even numbers:

my $even = &even_numbers(1000); while (my $n = $even->()) { print "$n\n"; sleep(1); } sub even_numbers { my $number = shift; $number-- unless ($number % 2); return sub { $number += 2 }; }

I know this is not the most sophisticated example, but you got the idea.

If you want to learn more about iterators, this is a good start point:

http://www.perl.com/pub/a/2005/06/16/iterators.html

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://539233]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.