Front page | perl.perl5.porters |
Postings from February 2011
[perl #84786] $tied_hash{overloaded_object} += 3; fails
Thread Next
From:
Eric Brine
Date:
February 23, 2011 13:09
Subject:
[perl #84786] $tied_hash{overloaded_object} += 3; fails
Message ID:
rt-3.6.HEAD-24085-1298495345-1623.84786-75-0@perl.org
# New Ticket Created by "Eric Brine"
# Please include the string: [perl #84786]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=84786 >
Hi,
Given a tied hash that contains a value which is an object with overloads,
the following code attempts to numify the overloaded object:
$tied_hash{overloaded_object} += 3;
The overloaded '+=' isn't getting called. The following works fine:
$tied_hash{overloaded_object} = $tied_hash{overloaded_object} + 3;
$plain_hash{overloaded_object} = $plain_hash{overloaded_object} + 3;
$plain_hash{overloaded_object} += 3;
Tested with v5.12.2 and a recent blead (v5.13.9-284-g76cc22e).
Test case and output below.
- Eric Brine
----- BEGIN CODE -----
#!/usr/bin/env perl
use strict;
use warnings;
{
package Test;
use overload
'+' => \&append,
'+=' => \&append;
sub append {
my ( $self, $value ) = @_;
push @$self, $value;
return $self;
}
}
use Test::More tests => 3;
use Tie::Hash qw( );
{ # Works for "+" instead of "+=".
my %hash;
tie %hash, 'Tie::StdHash';
$hash{'test'} = bless [], 'Test';
eval { $hash{'test'} = $hash{'test'} + 3; 1 }
or diag("$@");
is(3, $hash{'test'}[0], '+, tied hash');
}
{ # Works for untied hash, so it's not a problem with the overloaded object.
my %hash;
$hash{'test'} = bless [], 'Test';
eval { $hash{'test'} += 3; 1 }
or diag("$@");
is(3, $hash{'test'}[0], '+=, normal hash');
}
{
my %hash;
tie %hash, 'Tie::StdHash';
$hash{'test'} = bless [], 'Test';
eval { $hash{'test'} += 3; 1 }
or diag("$@");
is(3, $hash{'test'}[0], '+=, tied hash');
}
----- END CODE -----
----- BEGIN OUTPUT -----
1..3
ok 1 - +, tied hash
ok 2 - +=, normal hash
# Operation "0+": no method found, argument in overloaded package Test at
a.pl line 50.
not ok 3 - +=, tied hash
# Failed test '+=, tied hash'
# at a.pl line 53.
# got: '3'
# expected: undef
# Looks like you failed 1 test of 3.
----- END OUTPUT -----
Thread Next
-
[perl #84786] $tied_hash{overloaded_object} += 3; fails
by Eric Brine