Masem has asked for the wisdom of the Perl Monks concerning the following question:
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.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();
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Combined tied-array / object problem
by tilly (Archbishop) on Nov 11, 2001 at 07:14 UTC | |
by TheDamian (Vicar) on Nov 12, 2001 at 02:00 UTC | |
|
Re: Combined tied-array / object problem
by Aristotle (Chancellor) on Nov 11, 2001 at 07:44 UTC | |
|
Re: Combined tied-array / object problem
by chipmunk (Parson) on Nov 11, 2001 at 21:30 UTC |