develooper Front page | perl.perl5.porters | Postings from August 2008

[perl #57500] Passing object around affects += overloading

Thread Next
Peter Scott
August 2, 2008 04:31
[perl #57500] Passing object around affects += overloading
Message ID:
# New Ticket Created by  Peter Scott 
# Please include the string:  [perl #57500]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This occurs on at least 5.8.8 and 5.10.0.  Make two objects from a 
package with += overloaded (and nothing else), then += them.  If you do 
that directly after construction, no problem.  If you do that after 
passing the objects through some intermediate assignment, e.g. as hash 
values, the runtime error

   Operation "=": no method found, argument in overloaded package

is emitted.

   my $bar_x = Bar->new;
   my $bar_y = Bar->new;

   # No problem:
   $bar_x += $bar_y;   # 42

   my $x = { bar => Bar->new };
   my $y = { bar => Bar->new };
   my $der_x = $x->{bar};
   my $der_y = $y->{bar};

   # Operation "=": no method found, argument in overloaded package Bar:
   $der_x += $der_y;

   package Bar;

   use overload '+=' => sub { 42 };

   sub new { return bless {}, shift }

The objects $bar_x and $der_x look identical (pace addresses) under 
Devel::Peek, and -Do prints exactly the same thing before each += 
operation.  If this behavior is mentioned anywhere online or in documentation it eludes me.  Workaround appears to be to add

	'=' => sub { shift }

to the overload.

Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
     osname=linux, osvers=2.6.16-1.2133_fc5, archname=i686-linux
     uname='linux tweety 2.6.16-1.2133_fc5 #1 tue jun 6 00:52:14 edt 
2006 i686 athlon i386 gnulinux '
     config_args='-des -DDEBUGGING'
     hint=recommended, useposix=true, d_sigaction=define
     useithreads=undef, usemultiplicity=undef
     useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
     use64bitint=undef, use64bitall=undef, uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
     cc='cc', ccflags ='-DDEBUGGING -fno-strict-aliasing -pipe 
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 
     optimize='-O2 -g',
     cppflags='-DDEBUGGING -fno-strict-aliasing -pipe 
-I/usr/local/include -I/usr/include/gdbm'
     ccversion='', gccversion='4.1.1 20060525 (Red Hat 4.1.1-1)', 
     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
     ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', 
     alignbytes=4, prototype=define
   Linker and Libraries:
     ld='cc', ldflags =' -L/usr/local/lib'
     libpth=/usr/local/lib /lib /usr/lib
     libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
     perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
     libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
   Dynamic Linking:
     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
     cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib'

Characteristics of this binary (from libperl):
                         USE_LARGE_FILES USE_PERLIO
   Built under linux
   Compiled at Dec 26 2007 19:03:58
Peter Scott
Pacific Systems Design Technologies

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About