in reply to Overloading print()

As I understand it, use overload '""'=> \&my_quote; will overload both "" and print

That isn't quite right. The print operator as such cannot be overloaded. Since print always stringifies its arguments, you see the effect of overloaded '""' whenever such an object is printed.

If you need special processing on an object, provide a method special (using a more reasonable name) and call it when printing

print $obj->special, "\n";
Nothing new here, of course.

On an unrelated note, there is another pitfall built into '""' overloading. If you allow autogeneration of overload methods (which is the default), overload will autogenerate a method for overloading of 'bool' based on stringification. That may come as a surprize if you expect objects to always represent a true value as is normally the case. I routinely "counter-overload" 'bool' as

use overload ( '""' => 'stringify', # or whatever bool => sub { 1 }, # ... );
Alternatively switch off autogeneration altogether.

Anno

Replies are listed 'Best First'.
Re^2: Overloading print()
by syphilis (Archbishop) on Sep 06, 2007 at 05:30 UTC
    Since print always stringifies its arguments, you see the effect of overloaded '""' whenever such an object is printed

    Aaah ... I *now* see the connection. Thanks for the clarification.

    Interesting that you mentioned 'bool'. I hadn't overloaded it so, as you pointed out, any boolean evaluations were based on what the overloaded "" returned. I was aware that was happening - and it was pretty much doing what I wanted (namely, returning true unless the string was "0"). It then occurred to me (only a couple of days ago) that nans and -0 would also return true - since the strings "@NaN@" and "-0" return true. That's not what I wanted, so I've now explicitly overloaded bool to behave as I want.

    Cheers,
    Rob