Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Combining "tie" and Inside-Out Objects

by rjray (Chaplain)
on Sep 27, 2006 at 06:55 UTC ( #575096=perlquestion: print w/replies, xml ) Need Help??

rjray has asked for the wisdom of the Perl Monks concerning the following question:

This isn't a typical "I need help" post, more of an invitation to offer suggestions and thoughts. It just didn't seem deep-enough to go under "Meditations".

For my WebService::ISBNDB package, I'm working on implementing a HOP-style iterator to return when the user does a search. Some searches, especially when vague, could return well into the hundreds (if not thousands) of matches. Add to that the fact that the service only returns data in pages of 10, and an iterator that hides all this hassle from the user makes sense.

What also makes sense, though, is expecting something like that sort of search to return an array, or at least an array reference; one that can be manipulated with pop, shift, etc. Even push and unshift, though in this case I would imagine one might not want to allow adding to such an array (you can always copy what you want to a separate array and add in whatever else you need).

Enter tied arrays. The TIEARRAY method doesn't have to return an array reference. Even the perltie man-page uses a hash-reference for the examples under "Tying Arrays".

Enter, from the other side of the stage, that new darling of the Perl OO community, the inside-out object. Which I've enjoyed working with in this very project. So why not try getting some of that peanut butter in my chocolate? Obviously TIEARRAY can return an IOO. And when you call tie, it has an actual return value, the referent that TIEARRAY returns (or TIESCALAR, TIEHASH, etc. as apropos). You could (conceivably) then choose to use the core object with methods like next, first or all. Or you could use the tied array, with the usual array-related operations.

Of course, there would be issues whether or not you wanted the "array" to be mutable via slicing operations, what to do if the user addresses both the iterator and array interfaces, etc. But it gets me to thinking, and that often comes to no good...

--rjray

Replies are listed 'Best First'.
Re: Combining "tie" and Inside-Out Objects
by xdg (Monsignor) on Sep 27, 2006 at 10:54 UTC

    I don't see why this wouldn't work from a technical perspective. Inside-out objects are just objects, after all. Do you have a particular concern about it? Or sample code?

    I think the bigger question is whether a tied array is really the right interface. If you want pop and shift semantics, why not use methods?

    $obj->shift() $obj->pop()

    From the sound of it, though, you don't really want full array semantics, you just want the sugar of writing 'shift @tied'. On a pop, do you really want to exhaust and cache the iterator to find the last value to return? Do you want it to be expanded in a for loop:

    for my $match ( @tied_result ) { # ... }

    It seems like tying an array creates more interface confusion and likelihood for misuse.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?