I took that as a challenge!

Tie::ArrayAsHash:

{ package Tie::ArrayAsHash; use strict; no warnings; use Carp; use Hash::FieldHash qw(fieldhash); use Scalar::Util qw(reftype); use base qw(Exporter); BEGIN { our @EXPORT_OK = 'aeach'; $INC{'Tie/ArrayAsHash.pm'} = __FILE__; }; use constant { IDX_DATA => 0, IDX_EACH => 1, NEXT_IDX => 2, }; fieldhash my %cache; sub aeach (\[@%]) { my $thing = shift; return 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 }; 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 $k+0 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); } }

Usage:

use 5.010; use Tie::ArrayAsHash 'aeach'; my %hash = qw( a foo b bar c baz ); my @array = qw( foo bar baz ); while (my ($key, $value) = aeach %hash) { say "HASH $key => $value"; } while (my ($idx, $value) = aeach @array) { say "ARRAY $idx => $value"; }

Basically it's Perl 5.12's each, but should work in Perl 5.8+. But it's called aeach.

It is possible to define a sub called each but it's kinda awkward to actually use it.

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

In reply to Re: Making each(@ary) work on 5.10? by tobyink
in thread Making each(@ary) work on 5.10? by sedusedan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.