Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Arrays are special with foreach. They're not slurped, but iterated one by one. You could tie an array to each and iterate over the hash keys without slurping. But then, a simple while loop would be easier :)

    To get a lazy list in Perl 5, simply tie. You can have an infinite list and iterate over it using foreach, as long as you implement the list as an array.

    package OneToInf;
    # Assumes sequential contiguous access :)
    sub TIEARRAY { bless \(my $foo = 0) }
    sub FETCHSIZE { ${+shift} + 1 }
    sub FETCH { ${+shift}++ }

    package main;
    tie my @array, 'OneToInf';
    foreach (@array) {
        print "$_\n";
    }