NAME

PerlX::Range - Lazy Range object in Perl 5

SYNOPSIS

  use PerlX::MethodCallWithBlock;
  use PerlX::Range;

  my $a = 1..5000;

  $a->each {
      # $_ is the current value

      return 0 if should_break($_);
  };

DESCRIPTION

PerlX::Range is an attemp to implement make range operator lazy. When you say:

    my $a = 1..10;

This `$a` variable is then now a PerlX::Range object.

Notice here that it's `$a` but not `@a`. PerlX::Range overrides the behavior of `..` operator to construct one PerlX::Range object. You can deparse it to see what it's changed to:

    > perl -MPerlX::Range -MO=Deparse -e '$a=1..3'
    BEGIN {
        $^H{'feature_say'} = q(1);
        $^H{'feature_state'} = q(1);
        $^H{'feature_switch'} = q(1);
        $^H{'PerlXRange'} = q(1);
    }
    $a = PerlX::Range::xrange(1, 3);
    -e syntax OK

It also enables those 5.10 features for its caller, for the love of them.

At this point the begin of range can only be a constant, and better only be a number literal.

At this point the min/max value of range are assumed to be numeric literals or variables.

THE SYNTAX

The 0.04 version of PerlX::Range added the syntax of using * and :by(x) like Perl6, however, they are temporarily removed in the current version due to a total re-write of PerlX::Range with XS. The XS based implementation should work less problematic and robust. However, it's more difficult to extend syntax with pure XS. I still suck at XS, so github pull requests are very welcome. :)

METHODS

min, from, first

Retrieve the minimum value of the range.

max, to, last

Retrieve the maximum value of the range.

each($cb)

Iterate over the range one by one, the $cb should be a code ref. Inside the body of that, $_ refers to the current value.

If you want to stop before it reach the end of the range, or you have to because the range is infinite, you need to say return 0. A defined false value from $cb will make the iteration stop.

by($x)

Specify the step distance. So Here's how you define some odd numbers:

    # 1, 3, 5, 7, 9
    my $a = 1..10;
    $a->by(2);

Or, in one line:

    my $a = (1..10)->by(2);

Due to the fact that -> operator is tighter then .., 1..10 needs to be written in a pair of parens.

AUTHOR

Kang-min Liu <gugod@gugod.org>

LICENSE AND COPYRIGHT

Copyright (c) 2009, Kang-min Liu <gugod@gugod.org>.

This is free software, licensed under:

    The MIT (X11) License

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.