NAME

Tie::Diamond - Iterate the diamond operator via a Perl array

VERSION

version 0.04

SYNOPSIS

use Tie::Diamond;
tie my(@ary), "Tie::Diamond" or die;
while (my ($idx, $item) = each @ary) {
    ...
}

# to autochomp lines ...
tie my(@ary), "Tie::Diamond", {chomp=>1} or die;

DESCRIPTION

This class lets you iterate the diamond operator via a Perl array. Currently the only useful thing you can do with the array is just iterate it using each(), as shown in Synopsis. To be more exact, the class currently only implements FETCH() and FETCHSIZE().

The array backend does not slurp all lines into memory (or store past lines at all, actually), so it's safe to iterate over gigantic input.

TIE() OPTIONS

Options are passed as a hashref. Known keys:

  • chomp => BOOL (default 0)

    If set to true, lines will be chomp()-ed.

FAQ

Why?

So you can iterate using each(), basically, or to be compatible with a normal Perl.

One of my modules, Data::Unixish, has functions that accept array. It can either an actual Perl array (to iterate over a in-memory structure), or a tied array (to iterate lines from STDIN/files). The functions do not need to care; they can just use each().

Can I slurp?

@other = @ary; # or print @ary

Currently no. And anyway, if you want to slurp all lines, you might as well just do:

@other = <>; # or print <>

and skip this class altogether.

SEE ALSO

Iterator::Diamond

Tie::File

Syntax::Feature::EachOnArray if you are using Perl older than 5.12.

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.