Re: Is this possible to override print() ?



On Mon, Feb 25, 2008 at 9:43 PM, Panda-X <exilepanda@xxxxxxxxx> wrote:
Sorry... the email is unfinished, the reason why I need to do this is
because
I don't want to pollute the existed codes / context ... the original print
statements would
like to retain, but I can still do the translations by adding :
use OverridePrint;
in a common module that all existed scripts are now using.
snip

You can find out whether or not a core function can be overridden by
calling the prototype* function with that function's name prepended
with CORE::. If it returns a prototype, then you can override it, if
it returns undef, then you cannot. Unfortunately for you, print can
not be overriden (most likely because of its indirect object syntax
file handle wonkiness); however, there is another way: source filters.
You can use Filter::Simple to replace every call to print with a call
to my_print. Please note that it isn't quite that simple though. You
will need to correctly handle lines like

print $message;
print $log $message;
print STDERR "oops\n";

by converting them into something like

my_print(args => [$message]);
my_print(file => $log, args => [$message])
my_print(file => \*STDERR, args => ["oops\n"]);

and those were the simple cases.

* http://perldoc.perl.org/functions/prototype.html
** http://search.cpan.org/dist/Filter-Simple/lib/Filter/Simple.pm
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.



Relevant Pages

  • Re: Is this possible to override print() ?
    ... I don't want to pollute the existed codes / context ... ... but I can still do the translations by adding: ... package OverridePrint; ...
    (perl.beginners)
  • Re: Is this possible to override print() ?
    ... I don't want to pollute the existed codes / context ... ... use OverridePrint; ... there is another way: source filters. ... whatever special behaviour you need for a tied filehandle ...
    (perl.beginners)