|XP is just a number|
Are array based objects fixed length?by pdcawley (Hermit)
|on Jul 26, 2002 at 09:12 UTC||Need Help??|
pdcawley has asked for the
wisdom of the Perl Monks concerning the following question:
I know the theoretical answer to that -- No, array based objects do not have to have a fixed length. However, some background.
I'm working on yet another object persistence tool, called Pixie (blame Leon Brocard for the name, not me). One of the goals of the pixie project is to make things as simple as possible for the user (but, of course, no simpler). Amongst other things this means we want to supply simple locking.
The idea is that, if you're using a locking pixie, then fetching an object from the store will automatically grab a lock on it. That lock needs to be released at DESTROY time. This level of locking is, of course, simplistic, but it ensures that there should never be two 'active' copies of an object in play, which is generally a good thing.
So, how to handle releasing stuff at DESTROY time?
With a hash based object, things are relatively simple:
So, when the fetched object goes out of scope, so does the Pixie::ObjectInfo it contains, which handles unlocking the object in the database (along with any other cleanup that pixie needs to do). Which is rather cute, though I do say so myself. Note that the above code is rather simplified compared to what's actually in Pixie.
So, what happens when the class is a blessed arrayref? If we could rely on all pixie client classes to use fixed length arrays then it'd be a doddle, we could just do
But that means we have to assume that the array length is fixed and that the class's implementation will never use negative indices into the array.
This seems to be a little to sweeping as assumptions go... There's always the trick of blessing the thing into a new 'pseudo anonymous' class and hanging the behaviour on there, but it's not exactly pretty.
So, what do you think? I'm especially interested in hearing from people who've actually used array based classes in anger. Do you use fixed length arrays? negative indices? Is there a major CPAN class that breaks either of these assumptions?