Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^5: Getting for() to accept a tied array in one statement

by Veltro (Hermit)
on Apr 16, 2019 at 14:09 UTC ( [id://1232657] : note . print w/replies, xml ) Need Help??


in reply to Re^4: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement

Yes, my point is that you simply can't assume an object can be de-referenced to an array. The tie mechanism useful for creating algorithms that can handle objects without implying knowledge about their implementation. Otherwise this would defeat the whole purpose using tie. So it is still a good question, any other solutions appreciated.

Replies are listed 'Best First'.
Re^6: Getting for() to accept a tied array in one statement
by hdb (Monsignor) on Apr 16, 2019 at 14:12 UTC

    But that statement refers to the reference returned by tie which is not an array reference. The tied array works like a normal array.

      I don't understand, what you call 'not an array reference' is what I call 'object', so don't we mean the same thing?

      In:

      use ar ; my @ar ; my $art = tie @ar, "ar" ; @ar = (1, 2) ;

      My point is, I cannot know what $art is, so to me this is a big no no:

      my @ar-deref = @{$art};

        Agree. But the following is perfectly ok IMHO

        for(@ar) { print "$_\n"; }
        My point is, I cannot know what $art is, so to me this is a big no no: my @ar-deref = @{$art};

        Yes, I agree that is bad (unless $art overloads @{}), but in the code you showed here, for (tie @ary, "My::Class"), there is no such "bad" de-reference of the return value of tie happening, not even an implicit one. for (tie @ary, "My::Class") { print "$x\n" } will just print something like My::Class=HASH(0x...) - as perlancar said, it's just looping over a one-element list, the return value of tie, and not the elements of @{$art}, as you seemed to be worried about.