[Dbix-class] String evaluation in resultset->next

Peter Rabbitson rabbit+dbic at rabbit.us
Tue Nov 18 11:23:58 GMT 2014


On 11/18/2014 10:58 AM, Christian Lackas wrote:
> Hi Everybody,
>
> I frequently use
>
>      use overload '""' => sub { shift->toString }, fallback => 1;
>
> for my Row classes (via a role).
>
> ...
>
> However, besides the warnings, I am also wondering if there is a
> performance penalty by using stringify without actually needing it.
>

This part of your question deals with the main problem - you have 
defined *only* stringification in combination with fallback, so some 
things are autogenerated (including boolification as described in 
https://metacpan.org/pod/overload#Magic-Autogeneration and also the last 
sentence of 
https://metacpan.org/pod/overload#Minimal-Set-of-Overloaded-Operations). 
This has nothing to do with next(): consider the following "one-liner"

~$ perl -MScalar::Util -e '

   sub refdesc ($) {
     sprintf "%s%s(0x%x)",
       ( defined( $_[1] = Scalar::Util::blessed $_[0]) ? "$_[1]=" : "" ),
       Scalar::Util::reftype $_[0],
       Scalar::Util::refaddr($_[0]),
     ;
   }

   {
     package One;
     use overload
       bool => sub { warn "Called boolification on " . ::refdesc $_[0]},
       q("") => sub { warn "Called stringification on " . ::refdesc $_[0] },
       fallback => 1
     ;
   }

   {
     package Two;
     use overload
       q("") => sub { warn "Called stringification on " . ::refdesc $_[0] },
       fallback => 1
     ;
   }

   {
     package Three;
     use overload
       q("") => sub { warn "Called stringification on " . ::refdesc $_[0] },
     ;
   }

   {
     package Four;
     use overload
       q("") => sub { warn "Called stringification on " . ::refdesc $_[0] },
       fallback => 0,
     ;
   }

   for my $obj ( map { bless {}, $_ } qw(One Two Three Four) ) {
     my $is_false = !$obj;
   }
'


> Also, I believe this next-behavior is also responsible for a 'deep
> recursion' error I see in a more complex Template Toolkit usage (at
> least I see similar warnings and using a for loop worked around the
> issue; have not fully investigated this yet, though).

Note in the script above I did not write:
   ... q("") => sub { warn "Called stringification on $_[0]" }

but instead added a sub to properly format the ref without touchinbg the 
overloads.
I am not 100% sure but you may be facing a similar issue - you are 
stringifying within your stringification sub.

Cheers



More information about the DBIx-Class mailing list