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


In reply to Re: Overloading print() by Anno
in thread Overloading print() by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.