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 $_; $_; }