Hello everyone,

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+'!

$hash{'test'} += 3;
These work:
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,
Rob
#!/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]); }; # ???

In reply to Mixing tie %hash and overload results in strange behavior by robhoelz

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.