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

Christian Lackas lackas at lackas.net
Tue Nov 18 16:23:00 GMT 2014


* Peter Rabbitson <rabbit+dbic at rabbit.us> [141118 06:25]:

Dear Peter,

thanks for your excellent response.
Was indeed overlooking that I of course do have a boolean context in the
while loop, and did not realized it is derived from my string operator,
rather than continuing to use the regular scalar bool operator.

Add an explicit bool operator did the trick.

Thanks,
 Christian

-- 
Dr. Christian Lackas, Managing Partner
inviCRO, LLC -- In Imaging Yours
http://www.invicro.com/  http://www.spect-ct.com/

> 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
> 
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk



More information about the DBIx-Class mailing list