Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Redefining print

by suaveant (Parson)
on Feb 08, 2002 at 18:10 UTC ( #144178=perlquestion: print w/replies, xml ) Need Help??

suaveant has asked for the wisdom of the Perl Monks concerning the following question:

I know, I know, it's not the best thing in the world to do, but for the purposes of debugging a large cgi app I want to redefine the global print to try to track down a problem. I assume it can be done... but mainly I am not sure at all what prototype I need for print... can anyone shed some light on the process?

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

Replies are listed 'Best First'.
(RhetTbull) Re: Redefining print
by RhetTbull (Curate) on Feb 08, 2002 at 19:12 UTC
    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 $_; $_; }
      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)

Re: Redefining print
by Juerd (Abbot) on Feb 08, 2002 at 20:49 UTC
(crazyinsomniac) Re: Redefining print
by crazyinsomniac (Prior) on Feb 09, 2002 at 09:58 UTC
Re: Redefining print
by theorbtwo (Prior) on Feb 08, 2002 at 19:15 UTC

    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.

Re: Redefining print
by dmmiller2k (Chaplain) on Feb 08, 2002 at 18:47 UTC

    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.


    If you GIVE a man a fish you feed him for a day
    TEACH him to fish and you feed him for a lifetime

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://144178]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-04-13 00:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found