First, for some background, see Object and tied hash all at once? on the ability to make a scalar ref act as both a hash and an object. I'm looking for something similar with an array and an object, and while the basic setup seems ok, I'm running into problems when I'm trying to expand the functionality of the tied object.
The basic code I'm using is:
package Test; use Tie::Array; use Exporter(); @ISA = qw( Exporter Tie::StdArray ); sub new { my $class = shift; my @self; tie @self, $class; bless \@self, $class; } sub test { print "hello world\n"; } sub test2 { my $self = shift; print $self->{ test }, "\n"; # LINE 22 } sub TIEARRAY { my $class = shift; return bless { test => [] }, $class; } sub STORE { my $class = shift; my $index = shift; my $value = shift; $self->{ test }->[ $index ] = $value; } sub FETCH { my $class = shift; my $index = shift; return $self->{ test }->[ $index ]; } my $test = new Test(); $test->[0] = "test"; $test->test(); $test->test2();
Everything up to the second last line works, but when test2() is executed, I get "Can't coerce array into hash" at line 22 as indicated. Data::Dumping the $self value in the object functions shows what looks like a tied object, that is, an array and a class name. I've tried playing with tied, but with no luck.

I am looking to get to the interface to this class as in the code at the bottom of the file. Since I need to allow the user to use the array as a true array, I can't simply create a class and override []. I need to have added data storage in this case for extra varibles that can be set. I believe that a possible workaround would be to store a hashref in the last element of the tied array, and use the various tie functions to avoid the user from accessing this while having them available to the class, but this seems hack-ish.

Any suggestions on where to go from this?

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to Combined tied-array / object problem by Masem

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.