[Melbourne-pm] overloading 'print'

Mathew Robertson mathew.robertson at netratings.com.au
Tue Mar 21 16:26:07 PST 2006


The main point of the exercise was so that I could do something like:

 use MyPrint;
 print "Blah";

which outputs:

 MyPrint: Blah

rather than:

 Blah

The purpose being that I simply want to delete the 'use MyPrint;' line 
to restore the normal Perl behaviour.
Using IO::Wrap doesn't give that as I would have to change the syntax to:

 use IO::Wrap;
 no warnings;
 sub IO::Wrap::print {
   my $self = shift;
   print { $$self } "IO::Wrapped: ", @_;
 }
 use warnings;
 wraphandle(\*STDOUT);
 print "Blah";

or something similar.  However, this doesn't override 'print' as it 
produces the output:

 Blah

rather than what is wanted:

 IO::Wrapped: Blah

Hope this explains,
Mathew

Joshua Goodall wrote:
> On Wed, Mar 22, 2006 at 10:32:10AM +1100, Mathew Robertson wrote:
>   
>> Thats an interesting question, and one that I will eventually tackle.   
>> The whole concept of overriding 'print' itself is fundamentally suspect, 
>> so I'm specifically trying to *not* allow flexible useage of the package.
>>     
>
> was there a reason you couldn't just create a wrapper class derived
> from IO::Wrap or whatever?  The wrapper class could then just munge
> the print call on the way through.
>
> depends on your code architecture I guess, but the OO way is nice :)
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/melbourne-pm/attachments/20060322/28b01e2f/attachment.html


More information about the Melbourne-pm mailing list