in reply to Array copy or encapsulation break?

Update:I forgot to mention part of the deal with the big list, its actually a hashref and i want to return the keys. So its something like:
sub GetBigList { my $self = shift; return keys( %{$self->{'BigList'}} ); }
keys() references the elements, but when the list is returned, a copy is made.

But as was mentioned above, i could use an iterator, in this case, each() could work. I may want to have a reset method though.

Replies are listed 'Best First'.
Re: Re: Array copy or encapsulation break?
by BrowserUk (Patriarch) on Aug 06, 2003 at 21:41 UTC

    If the list is a hash, you could tie that hash to your your package and give the caller the tied handle. That way, they can manipulate the hash with all the familiar hash operators (keys, values, each etc. and the standard method of resetting these iterators), but you get to intervene in every operation and vet what they do.

    Using ties this way makes for a nice perlish and familar standard interface to class attributes, including using them as lvalues, without relinguishing control. Ties aren't the fastest things in the world, but they're no slower than calling setters and getters.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.