in reply to Re: Caller, caller, wherefore art thou, caller?
in thread Caller, caller, wherefore art thou, caller?

Of course the current implementation already is an iterator – if you need several, you can just instantiate any number of Array::AsHash objects on the same underlying array.

If Ovid prefers this, he could facilitate it by providing a ->copy constructor which resets the iterator in the new instance, for use by code which has no access to the original underlying array.

However, assuming the class does not contain more methods, I wonder why an object is at all necessary for this. I’d just bake closure directly:

my $each = make_array_iterator \@array; while( my( $idx, $val ) = $each->() ) { # ... }

That seems far more natural to me.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Caller, caller, wherefore art thou, caller?
by adrianh (Chancellor) on Oct 11, 2005 at 07:57 UTC
    Of course the current implementation already is an iterator – if you need several, you can just instantiate any number of Array::AsHash objects on the same underlying array.

    True, but when Ovid said (my emphasis):

    I'm working on some code which lets me do, amongst other things

    I assumed that iterators were only one part of what he wanted Array::AsHash to do, so it still might make good sense to abstract out the iterator part.