Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Making each(@ary) work on 5.10?

by sedusedan (Monk)
on Jul 27, 2012 at 09:00 UTC ( [id://984005]=note: print w/replies, xml ) Need Help??


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

I don't think you're entirely correct. The code which I posted (feature::each_on_array) works on my 5.10. And I did not override "each" globally, only on the caller's package.

% perl -I. -Mfeature::each_on_array -E'say $^V; @a = (qw/a b c/); whil +e (($i, $e) = each @a) { say $i, $e }' v5.10.1 0a 1b 2c
vs
% perl -I. -Mfeature::each_on_array -E'say $^V; @a = (qw/a b c/); whil +e (($i, $e) = each @a) { say $i, $e } package Foo; @b = (qw/a b c/); +while (($i, $e) = each @b) { say $i, $e }' Type of arg 1 to each must be hash (not array dereference) at -e line +1, near "@b) "

Perl here does call our subroutine's each.

I'll be packaging this as a CPAN module; I need this to work on a 5.10 box, and I want to see CPAN Testers result. I'm including Tie::ArrayAsHash for now.

Replies are listed 'Best First'.
Re^5: Making each(@ary) work on 5.10?
by tobyink (Canon) on Jul 27, 2012 at 13:10 UTC

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

    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'
      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'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://984005]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-03-28 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found