dragonchild has asked for the wisdom of the Perl Monks concerning the following question:

(This is with 5.8.8 on OSX.)

I'm working on getting DBM::Deep to properly handle a edgecase and I ran into an edgecase with overload and re-blessing objects. The code below demonstrates the problem.

{ package Null; use overload bool => sub { undef }, '""' => sub { undef }, '0+' => sub { undef }, fallback => 1, nomethod => 'AUTOLOAD'; sub AUTOLOAD { return; } } { package Foo; sub new { return bless { a => 'b' }, shift; } sub apple { 'pear' } } my %x = ( foo => Foo->new, ); my $y = $x{foo}; print "Y1: $y\n"; bless $x{foo}, 'Null'; %{$x{foo}} = (); print "Y2: $y\n"; my $z = $x{foo}; print "Z: $z\n";
What I'm seeing:
Y1: Foo=HASH(0x1801234) Y2: Null=HASH(0x1801234) Z:
I would expect Y2 and Z to be the same. It's as if the overloading doesn't happen unless the SV is created after the blessing. Reblessing doesn't seem to trigger the overloading. Am I missing something?

For the record, what I'm trying to do is fix the following edgecase:

$db->{foo} = { a => 'b' }; my $x = $db->{foo}; my $y = $db->{foo}; ok( $x == $y ); # This is broken in 1.0004 and before, but fixed in SV +N. delete $db->{foo}; is( $x, undef ); # This works is( $x + 0, 0 ); # This does not
If someone has an idea of how to fix the underlying issue, I'm all ears.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re: Overload + reblessing oddity
by kyle (Abbot) on Sep 29, 2007 at 02:41 UTC

    Devel::Peek confirms the difference:

    use Devel::Peek; #...etc... bless $x{foo}, 'Null'; print Dump( $y ); print Dump( $x{foo} );

    $y has FLAGS = (PADBUSY,PADMY,ROK) and $x{foo} has FLAGS = (ROK,OVERLOAD).

    Fuller output:

    SV = RV(0x1822a6c) at 0x181c38c # $y REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x18014a0 SV = PVHV(0x1821f90) at 0x18014a0 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 1 NV = 0 STASH = 0x180b584 "Null" ARRAY = 0x301120 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "a" HASH = 0xca2e9442 SV = PV(0x1801678) at 0x181c4dc REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x301110 "b"\0 CUR = 1 LEN = 2 SV = RV(0x1822a74) at 0x181c4f4 # $x{foo} REFCNT = 1 FLAGS = (ROK,OVERLOAD) RV = 0x18014a0 SV = PVHV(0x1821f90) at 0x18014a0 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 1 NV = 0 STASH = 0x180b584 "Null" ARRAY = 0x301120 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "a" HASH = 0xca2e9442 SV = PV(0x1801678) at 0x181c4dc REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x301110 "b"\0 CUR = 1 LEN = 2

    I notice that the OVERLOAD flag is on the scalar itself rather than the reference it's holding. The reference knows that its an object but not that it's overloaded. I guess to get $y to be overloaded, you'd have to rebless it directly. That said, given a reference, I don't know how you'd find other scalars that have the reference.

    Update: See also Is it possible to obtain all hardreferences to some memory address by knowing only one of them?