in reply to Re^4: Making each(@ary) work on 5.10?
in thread Making each(@ary) work on 5.10?

I stand corrected. Here's the latest version, tested in Perl 5.8.9, 5.10.1 and 5.16.0...

package Tie::ArrayAsHash; use 5.008005; use strict; no warnings; BEGIN { $Tie::ArrayAsHash::AUTHORITY = 'cpan:TOBYINK'; $Tie::ArrayAsHash::VERSION = '0.001'; } use Carp; use Hash::FieldHash qw(fieldhash); use Scalar::Util qw(reftype); use Sub::Exporter -setup => { exports => [ 'aeach', 'each' => sub { \&aeach } ], groups => { auto => \&_export_list }, }; sub _export_list { my ($class) = @_; my %export = (aeach => \&aeach); $export{each} = \&aeach if $^V lt 5.11.0; return \%export; } use constant { IDX_DATA => 0, IDX_EACH => 1, NEXT_IDX => 2, }; fieldhash my %cache; sub aeach (\[@%]) { my $thing = shift; return CORE::each %$thing if reftype $thing eq 'HASH'; confess "should be passed a HASH or ARRAY" unless reftype $thing eq 'ARRAY'; my $thing_h = $cache{$thing} ||= do { tie my %h, __PACKAGE__, $thing; \%h }; CORE::each %$thing_h; } sub TIEHASH { my ($class, $arrayref) = @_; bless [$arrayref, 0] => $class; } sub STORE { my ($self, $k, $v) = @_; $self->[IDX_DATA][$k] = $v; } sub FETCH { my ($self, $k) = @_; $self->[IDX_DATA][$k]; } sub FIRSTKEY { my ($self) = @_; $self->[IDX_EACH] = 0; $self->NEXTKEY; } sub NEXTKEY { my ($self) = @_; my $curr = $self->[IDX_EACH]++; return if $curr >= @{ $self->[IDX_DATA] }; return $curr; } sub EXISTS { my ($self, $k) = @_; !!($k eq int($k) and $k < @{ $self->[IDX_DATA] } ); } sub DELETE { my ($self, $k) = @_; return pop @{ $self->[IDX_DATA] } if @{ $self->[IDX_DATA] } == $k + 1; confess "DELETE not fully implemented"; } sub CLEAR { my ($self) = @_; $self->[IDX_DATA] = []; } sub SCALAR { my ($self) = @_; my %tmp = map { $_ => $self->[IDX_DATA][$_] } 0 .. $#{ $self->[IDX_DATA] }; return scalar(%tmp); } 1;

The import options are these:

# Import nothing, but use the tied hash interface. use Tie::ArrayAsHash; # Import 'aeach'. use Tie::ArrayAsHash qw(aeach); # Import 'aeach', but call it 'each'. use Tie::ArrayAsHash qw(each); # Import 'aeach' with both names. use Tie::ArrayAsHash qw(-all); # Import 'each' if Perl lt 5.11.0; import 'aeach' regardless. use Tie::ArrayAsHash qw(-auto); # Import 'aeach' renamed it to whatever you like. use Tie::ArrayAsHash aeach => { -as => 'p512each' };

Still needs proper documentation and test suite, and maybe a better name.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^6: Making each(@ary) work on 5.10?
by sedusedan (Pilgrim) on Jul 27, 2012 at 14:08 UTC
    I have included the previous version of Tie::ArrayAsHash in feature::each_on_array (soon to be renamed to Syntax::Feature::*, as per mst's suggestion). If you decide to upload this to CPAN, I'll remove the inclusion and depend on that instead.

      tobyink finally twigs that sedusedan==SHARYANTO.

      I don't plan on uploading it. Probably better to bundle the module rather than inline it. Especially as you seem to be inlining it three times.

      Be aware that Syntax::Feature:: modules use a different mechanism for installing methods to their caller. They don't use import but instead use a method called install. My own Syntax::Feature::Maybe is a fairly simple demonstration.

      Be aware that the original version of Tie::ArrayAsHash has a bug in EXISTS. The second version fixes that. Also, the second version uses CORE::each internally within aeach mostly just to clarify what's going on, but also just in case some bizarro situation ends up making it recursively call itself when installed as CORE::GLOBAL::each.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'