As I said, there's a reason I called it aeach instead of each. You can define your own sub called each, but if you try to actually use it, you just get the Perl built-in. This goes right down to the Perl tokenizer.

There are only three ways to call your own each function: qualify your call with the package name (MyPackage::each(@array)), prefix it with an ampersand (&each(\@array) - note that prototype will be ignored, so you need to pass a reference), or call it as a method (but methods can't be called on unblessed arrays, unless you use autobox).

Now, it is palso ossible to overwrite the core each with your own:

use Tie::ArrayAsHash 'aeach'; BEGIN { *CORE::GLOBAL::each = \&aeach };

However, this is a global override rather than being package scoped or lexically scoped. (You might think that it would introduce infinite recursion because aeach internally calls each, but actually it does not. This is because aeach has already been compiled when the override happens, so is not affected by the override.)

To override each for just a lexical scope is a much harder proposition.

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

In reply to Re^3: 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.