NAME Compile::Generators - Write Python-like generators in Perl SYNOPSIS use Compile::Generators; sub gen_range :generator { my ($min, $max) = @_; my $incr; my $num = $min; while (not defined $max or $num < $max) { $incr = shift || 1; yield $num; $num += $incr; } } my $range = gen_range(50, 100); my $incr = gen_range(1); while (my $num = $range->($incr->())) { print "\$num => $num\n"; } DESCRIPTION Compile::Generators lets you define subroutines that return their code as a generator. You can then call the generator over and over until it returns undef. The generator can yield (return) a value from and then when you call it again it resumes right after the yield. USAGE Any subroutine marked with the a ":generator" attribute will have it's code wrapped into a closure and returned by the subroutine. Any yield statments will be replace with code to return/resume at that point. Any code before the first blank line in the sub will not be a part of the closure but will be exectuted when the sub is actually called. This means that any variables that are defined before the blank line will be *closed* by the generator sub. This module uses Module::Compile to compile the generators. Look inside the ".pmc" to see what is really happening. Since this module uses "goto" statements, you cannot "yield" inside a "for" loop. Perl does not allow this. However you *can* use "while" statements. AUTHOR Ingy döt Net COPYRIGHT Copyright (c) 2006. Ingy döt Net. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See