Dave's Free Press

Technology: Tie-STDOUT-1.0401


Older stuff

Tie::STDOUT - intercept writes to STDOUT and apply user-defined functions to them.


Tie::STDOUT - intercept writes to STDOUT and apply user-defined functions to them.


    use Tie::STDOUT
        print => sub {
            print map { uc } @_;
        printf => ... 
        syswrite => ... ;


This module intercepts all writes to the STDOUT filehandle and applies whatever function you desire to what would have gone to STDOUT. In the example above, any use of the print() function on this filehandle will have its output transmogrified into upper case.

You will have noticed that we blithely print to the default filehandle (which is almost always STDOUT) in the function we supplied. Relax, this doesn't cause an infinite loop, because your functions are always called with a *normal* STDOUT.

You may provide up to three user-defined functions which are respectively called whenever you use print(), printf() or syswrite() on the filehandle:

defaults to printing to the real STDOUT;

defaults to passing all parameters through sprintf() and then passing them to whatever the 'print' function is;

Defaults to going straight through to the real STDOUT.

You will note that the default behaviour is exactly the same as it would be without this module.

Because we have a sensible default for 'printf' and because syswrite is so rarely used, you will normally only have to provide your own code for 'print'.


Doesn't work on perl 5.6, because it seems that localising tied filehandles doesn't work.




I like to know who's using my code. All comments, including constructive criticism, are welcome. Please email me.


David Cantrell <david@cantrell.org.uk>


Copyright 2006 David Cantrell

This module is free-as-in-speech software, and may be used, distributed, and modified under the same terms as Perl itself.