use invective;  $#!+! or die;

I'm trying to get a thing, object, thingy
that will allow me to dereference it as
a scalar, array, or hash.    i.e. 

$thingy->[3]='bingo';
$thingy->{'four'}='bango';
(side note: is there an arrow notation for
scalar dereferencing? $scalar_ref->deref = 'bongo';)

anyway, some digging pointed me in the direction of
using overloading. "perldoc overload" has this excerpt
of code:

package two_refs;
use overload 
        '%{}'=>\&gethash,
        '@{}'=> sub { $ {shift()} }   ;

sub new {
        my $package=shift;
        bless \ [@_], $package;
}
sub gethash {
        my %h;
        my $self = shift;
        tie %h, ref $self, $self;
        \%h;
}

# skip tieing code:

package main;
my $bar = new two_refs 3, 4, 5, 6;
$bar->[2] = 11;
$bar->{two}==11 or die 'bad hash fetch';

so, the first problem is this: it don't work.
second, I'm having enough trouble trying to
understand overload, but then there is some 
basic perl stuff going on that I don't get.

starting with the basic perl stuff, what the heck is this?
        '@{}'=> sub { $ {shift()} }   ;
jumping jehosifat. shift gives you the "thingy",
I think. a ref on the shifted value gave me 'two_refs'.
what the heck are the parens? is that trying to 
do a sub call?  And then there's that fiegen 
$ way in the front that's just kind of hanging out,
which I think is trying to do a scalar dereference.
(on whatever is returned by the sub call to shift() ??)
but the underling thingy is an array to start with,
so I don't get why overload has to do anything at all
with array dereferencing. couldn't it just dereference
the array ref like normal? why go through a sub call?


and then there's this little ditty:
        bless \ [@_], $package;

[@_] creates an anon array and puts the paramaters into it.
but what the heck is that slash doing in there???
bless only takes two parameters. 
does the \ take a reference to the anon-array-ref ?
so it blesses a scalar reference to an array reference?
again, can anyone explain why?

Once I get a handle on the line noise, can anyone
explain what the heck a subroutine that is overloaded
for hash dereferencing should return?
should it  return a hash reference? a hash flattened list?
or does the key get passed in and the sub should look
up the key in the hash and return the value?

I'm running perl 5.6.0, if it makes a difference.

Thanks in advance,
Greg London

Reply via email to