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:

package Pixie::ObjectInfo; ... sub DESTROY { ... $self->store->unlock($self); } package Pixie; sub get { my $self = shift; my $object_id = shift; my $obj = $self->really_get($object_id); $obj->{__Pixie} = Pixie::ObjectInfo->new(oid => $object_id); }
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

$obj->[@$obj] = Pixie::Object::Info->new
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?


In reply to Are array based objects fixed length? by pdcawley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.