Re: Is this possible to override print() ?



From: "Chas. Owens" <chas.owens@xxxxxxxxx>
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.

No need for source filters. All you have to do is to implement
whatever special behaviour you need for a tied (magical) filehandle
(see
perldoc perltie
perldoc Tie::Handle
) and then create and select the filehandle:


use OverridePrint;
tie *FH, 'OverridePrint'; # plus whatever other parameters you need
select(FH);


Basicaly the whole point is that instead of overriding print() for
all filehandles you override it just for that one filehandle ...

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: Is this possible to override print() ?
    ... I don't want to pollute the existed codes / context ... ... use OverridePrint; ... You can find out whether or not a core function can be overridden by ... calling the prototype* function with that function's name prepended ...
    (perl.beginners)
  • 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)