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 $_;
$_;
}
| [reply] [d/l] |
| [reply] |
| [reply] |
| [reply] [d/l] |
$> more file
#!/usr/bin/perl -w
use strict;
_1: f();
_2: d();
_3: f();
sub f { print "F\n"; }
sub d { print "D\n"; f(); }
$>perl -d:Trace file
>> file:4: _1: f();
>> file:8: sub f { print "F\n"; }
F
>> file:5: _2: d();
>> file:9: sub d { print "D\n"; f(); }
D
>> file:9: sub d { print "D\n"; f(); }
>> file:8: sub f { print "F\n"; }
F
>> file:6: _3: f();
>> file:8: sub f { print "F\n"; }
F
The Devel::Trace link above will also yield Devel::TraceMethods, which was born here (a wheel of chromatic, which I reinvented last week), along with Devel::TraceLoad and Devel::TraceCalls. They're all worth a look, and the Trace(Calls|Methods) modules are very useful when working with parsers you're not familiar with (like Pod::Parser, which is how I stumbled upon all of these, after writing my own of course ;D -- the whole Devel:: tree is full of goodies)
Happy Coding!!
update:
This very good info to know, and will help you with debuggin your program, but an added feature, would be a duplication of the filehandles/sockets .... what I mean is, if your program opens a file/socket handle, and data goes over it, it'd be nice if -d:Trace would log that stuff as well ;)
| [reply] [d/l] |
I don't think you can. Not directly, at least. Look up the prototype function in perlfunc. (BTW, I note that this is the first time I've ever had call to use it.) It returns undef for CORE::print, meaning that it "does not really behave like a Perl function".
However, you might want to try making a GLOBAL::print and a IO::Handle::print funtion.
IIRC, there's some magic that makes bareword filehandles act like IO::Handle objects.
TACCTGTTTGAGTGTAACAATCATTCGCTCGGTGTATCCATCTTTG
ACACAATGAATCTTTGACTCGAACAATCGTTCGGTCGCTCCGACGC
| [reply] |
Excellent question! ++
The problem is how do you accept the optional filehandle, which must NOT be separated from the remaining parameters by a comma.
May be similar to the code block (e.g., {}) in map, sort, or grep.
Wish I could remember where I read about this.
dmm
If you GIVE a man a fish you feed him for a day
But, TEACH him to fish and you feed him for a lifetime
| [reply] [d/l] [select] |