http://qs321.pair.com?node_id=144189


in reply to Redefining print

Unfortunately I think print is one of the built-ins that can't be overloaded (though I don't recall where I read this). You don't state why you want to overload it but you can probably accomplish what you want using Filter::Handle to intercept output to filehandles (including STDOUT and STDERR).

For example, the following snippet is pulled from one of my programs. The filter sub does 2 things: 1. if it sees any text that matches a password, it replaces it with xxxxx (that means I can dump config info to a log file for debugging but not worry about sensitive data being strewn about) and 2. It prints a copy of everything sent to STDOUT and STDERR to a log file.

use Filter::Handle qw(subs); Filter \*STDOUT, \&filter_sub; Filter \*STDERR, \&filter_sub; sub filter_sub { #sub for Filter::Handle #filter out the password if we see it so password doesn't get stre +wn all over log files local $_ = "@_"; my $pass = $config{password}; s/$pass/xxxxx/g; #write everything to logfile then to pass to filtered file handle print LOGFILE $_; $_; }

Replies are listed 'Best First'.
Re: Re: Redefining print
by blakem (Monsignor) on Feb 08, 2002 at 19:45 UTC
Re(2): Redefining print
by suaveant (Parson) on Feb 08, 2002 at 19:44 UTC
    Ahhh... actually, you just made me realize that the most likely problem is coming from the data I print through a UNIX socket... and can probably confiscate that data easily. I am actually trying to track down a confusing "Premature end of script headers"

                    - Ant
                    - Some of my best work - (1 2 3)