in reply to A warning about overloading assignment methods

I think if i understand this properly you can see the same thing at work without overload. Try this:

package T; use Data::Dump::Streamer; sub foo { \$_[0] } sub bar { my $x=shift; \$x } my $o=bless [],'T'; Dump($o->foo,$o->bar,$o)->Names(qw(foo bar o))->Out(); __END__ $foo = \$o; $bar = \do { my $v = 'V: $o' }; $o = bless( [], 'T' ); $$bar = $o;

What happening here is that you seeing the difference between copy semantics (what happens with assignment) and aliasing semantics (what happens when you take a reference to an alias).

But i know you well enough to know this isnt a surprise to you, so Im wondering if you could explain in more detail perhaps with a minimal case we can run to see the effect you mean.

---
demerphq

Replies are listed 'Best First'.
Re^2: A warning about overloading assignment methods
by diotalevi (Canon) on Apr 20, 2005 at 08:43 UTC

    Here's my short test code which demonstrates the data structure prior to returning and how it instamagically turned into a self-ref afterward.

    Here's the data before returning and after returning it from the -= method.

    $expected = bless( \bless( \1, 'T' ), 'T' ); $got = bless( \$got, 'T' );

    And here's the sample code.

    package T; use Data::Dump::Streamer; sub D { my ( $name, $val ) = @_; Dump() ->Purity( 0 ) ->Names( $name ) ->Data( $val ) ->Out(); return; } use overload( '-=' => sub { my $new = bless \ $_[0], "T"; D( new => $new ); return $new; }, '""' => sub { my $self = shift; D( self => $self ); return $$self } ); sub new { my ( $class, $val ) = @_; bless \ $val, $class; } package main; $o = T->new( 1 ); $o -= 2; "$o";