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

Hi, I need to solve the following: there are a bunch of perl scripts calling a subroutine
WriteToLog all over the place. IT basically prints out the status of the program flow with time stamp and some other info
into a log file. But I only want those info for certain actions not always and I have identified where those 2 points
are. The WirteToLog is just too noisy. I am thinking of intercepting the WriteToLog call and with a flag, If the flag
is on it prints to log file, it not skips. I do not have access to WriteToLog source code. only the interface.
How to do this? the script layout: file a.pl
sub func1{ WriteTOLog($Arga,$Argb,$Argc,$Argd); } ... do a bunch of things <code> file b.pl <code> sub func2{ WriteTOLog($Arga,$Argb,$Argc,$Argd); } ... do a bunch of things <code> file c.pl <code> sub func3{ WriteTOLog($Arga,$Argb,$Argc,$Argd); } ... do a bunch of things <code> file log.pl <code> sub WriteToLog($,$,$,$){ local ($var) = @_; # do its fike IO stuff }
so can I change WriteTOLog to:
sub MyWriteToLog($,$,$,$){ if ($FLAG eq 1) { local ($var) = @_; # do its fike IO stuff WriteTOLog($a,$b,$c,$d); } else { #do nothing or other things; } }
how to make those proginal files call MyWriteToLog when they see WriteTOLog? D I really have to change all these files?

2006-02-10 Retitled by broquaint, as per Monastery guidelines
Original title: 'intercept soub routine calls'

Replies are listed 'Best First'.
Re: Intercept subroutine calls
by Fletch (Bishop) on Feb 10, 2006 at 00:33 UTC

    Add *WriteToLog = \&MyWriteToLog; after the declaration of MyWriteToLog (and after the declaration of WriteToLog as well, of course). I believe perldoc perlsub should cover this.