in reply to Overloading 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
Nothing new here, of course.print $obj->special, "\n";
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
Alternatively switch off autogeneration altogether.use overload ( '""' => 'stringify', # or whatever bool => sub { 1 }, # ... );
Anno
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Overloading print()
by syphilis (Archbishop) on Sep 06, 2007 at 05:30 UTC |