Dear monks,

I am trying to achieve the following functionality: I have an inside-out class where the underlying data structures of the objects are scalar references. I'd like to be able to access the contents referenced by objects from this class as arrays, i.e.:
use Foo::Bar; my $fb = Foo::Bar->new; $fb->[0] = 12; # here Foo::Bar::STORE($self,$index,$value) is called print $fb; # not sure, maybe Foo::Bar=SCALAR(0x1835bfc)?
Along similar lines, I'd like to be able to push @$fb, etc. (which would call the PUSH method). I have perused the documentation for perltie and overload and I think it must be some combination of that, but the best I could come up with was using:
use Foo::Bar; tie my @array, 'Foo::Bar'; $array[0] = 12; # here Foo::Bar::STORE($self,$index,$value) is called
...which is not really what I want. I want the constructor, data structure (scalar ref) and interface to remain the same, and just add syntax sugar. I'm guessing I have to call TIEARRAY in some way in the new constructor?

Thanks for any and all help!

In reply to tie'ing a scalar as an array? by rvosa

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.