http://www.perlmonks.org?node_id=1018030


in reply to Moose stringification overloading isn't working

Your output has two different classes in it:

Zep is              OT::Zeppelin::AddVendorKey 
stringifies as      OT::Zeppelin::AddVendorKey….
_stringify returns  OT::DB::Result::Upload...

So it looks like the line $this->row->ID can return two different objects.

The 'use overload' docs say:

The subroutines for '""' , '0+' , and 'bool' can return any arbitrary Perl *value*. If the corresponding operation for this *value* is overloaded too, the operation will be called again with this *value*.

As a special case if the overload returns the object itself then it will be used directly. An overloaded conversion returning the object is probably a bug, because you're likely to get something that looks like YourPackage=HASH(0x8172b34) .

Based on the order of the output, I would say that $this->row->ID is returning $this in some cases, and in other cases it is returning an object of the class OT::DB::Result::Upload, and that class has also overloaded '""'.

Here is a simple example of that:

use strict; use warnings; use 5.012; ############## { package Point; use Moose; # automatically turns on strict and warnings has 'x' => (is => 'rw', isa => 'Int'); has 'y' => (is => 'rw', isa => 'Int'); use overload '""' => \&_stringify; sub _stringify { state $count; my ($rhs, $_lhs, $swap) = @_; $count++; if ($count < 2) { return $rhs; } else { return D->new; } } } ################ { package Point3D; use Moose; extends 'Point'; has 'z' => (is => 'rw', isa => 'Int'); } ############### { package D; use Moose; use overload '""' => sub { my $class = ref shift; return "$class|hello" }; } ############### my $p3d = Point3D->new(x=>10, y=>20); say "$p3d"; say $p3d->_stringify; --output:-- Point3D=HASH(0x100bc5078) D|hello

Replies are listed 'Best First'.
Re^2: Moose stringification overloading isn't working
by 7stud (Deacon) on Feb 10, 2013 at 07:32 UTC
    What happens if you use double quotes twice? And what happens if you call _stringify() twice? Any difference?