robhoelz has asked for the wisdom of the Perl Monks concerning the following question:
I'm currently using Perl 5.12.3. I'm trying to mix tied hashes and overloaded objects, much to my own dismay. I overload the '+' operator in a package, and I store an array reference blessed into that package in a tied hash. Example:
my %hash; tie %hash, 'main'; $hash{'test'} = bless [], 'Test';
Now, when I do the following, I get an error about not having an overload for '0+'!
These work:$hash{'test'} += 3;
my $test = bless [], 'Test'; $test += 3; my %hash; tie %hash, 'main'; $hash{'test'} = bless [], 'Test'; $hash{'test'} = $hash{'test'} + 3;
I've read the overload documentation. I've tried (with no avail) specifying fallback. I've run it through a debugger, only to discover that it pukes with the '0+' error when attempting to return the value from FETCH. I've read some of the Perl source to try and figure out what's going on. Does anyone here have any insight to what I'm doing wrong? A provable script follows to illustrate what I'm trying to accomplish in code.
Thanks,#!/usr/bin/env perl package Test; use overload '+' => \&append; sub append { my ( $self, $value ) = @_; push @$self, $value; return $self; } package main; use strict; use warnings; use Test::More tests => 14; use Test::Exception; sub TIEHASH { my ( $class ) = @_; return bless {}, $class; } sub FETCH { my ( $self, $key ) = @_; return $self->{$key}; } sub STORE { my ( $self, $key, $value ) = @_; $self->{$key} = $value; } lives_ok { my %hash; tie %hash, __PACKAGE__; $hash{'test'} = bless [], 'Test'; isa_ok($hash{'test'}, 'Test'); }; lives_ok { my %hash; tie %hash, __PACKAGE__; my $test = bless [], 'Test'; $test += 3; $hash{'test'} = $test; is(3, $hash{'test'}[0]); }; lives_ok { my %hash; tie %hash, __PACKAGE__; $hash{'test'} = bless [], 'Test'; $hash{'test'} = $hash{'test'} + 3; is(3, $hash{'test'}[0]); }; lives_ok { my %hash; tie %hash, __PACKAGE__; $hash{'test'} = bless [], 'Test'; my $test = $hash{'test'}; $test += 3; $hash{'test'} = $test; is(3, $hash{'test'}[0]); }; lives_ok { my %hash; $hash{'test'} = bless [], 'Test'; $hash{'test'} += 3; is(3, $hash{'test'}[0]); }; lives_ok { my %hash; tie %hash, __PACKAGE__; $hash{'test'} = []; push @{$hash{'test'}}, 3; is(3, $hash{'test'}[0]); }; lives_ok { my %hash; tie %hash, __PACKAGE__; $hash{'test'} = bless [], 'Test'; $hash{'test'} += 3; is(3, $hash{'test'}[0]); }; # ???
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mixing tie %hash and overload results in strange behavior
by ikegami (Patriarch) on Feb 23, 2011 at 20:36 UTC | |
|
Re: Mixing tie %hash and overload results in strange behavior
by ikegami (Patriarch) on Feb 24, 2011 at 23:17 UTC |