<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME
    Compile::Generators - Python-like generator subroutines for Perl

SYNOPSIS
        use Compile::Generators;

        sub gen_range :generator {
            my ($min, $max) = @_;
            my $num = $min;
            my $incr;

            while (not defined $max or $num &lt; $max) {
                $incr = shift || 1;
                yield $num;
                $num += $incr;
            }
        }

        my $range = gen_range(50, 100);
        my $i = gen_range(1);

        while (my $num = $range-&gt;($i-&gt;())) {
            print "\$num =&gt; $num\n";
        }

    When run, this prints:

        $num =&gt; 50
        $num =&gt; 51
        $num =&gt; 53
        $num =&gt; 56
        $num =&gt; 60
        $num =&gt; 65
        $num =&gt; 71
        $num =&gt; 78
        $num =&gt; 86
        $num =&gt; 95

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 an empty list. The generator can yield (return) a value and then
    when you call it again it resumes right after the yield.

USAGE
    Any subroutine marked with the a ":generator" attribute will have its
    code wrapped into a closure and returned by the subroutine. Any yield
    statements 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 executed 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.

    Currently a "yield" statement needs to be a simple statement on it's own
    line, since this gets parsed and replaced by a return/goto-label.

    See the tests for examples.

AUTHOR
    Ingy d繹t Net &lt;ingy@cpan.org&gt;

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 &lt;http://www.perl.com/perl/misc/Artistic.html&gt;

</pre></body></html>