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

Hi Monks,

I have been working with a large perl code base. My requirement is to add a line to first and last line of every subroutine. ie., Lets say i want to print "Entering <subroutine-name>" to first line of every subroutine, and "Exiting <subroutine-name>" to every last line of subroutine (ofcourse, before the return statement).

How can i achieve this?

Thanks in advance,

arunvelusamy

  • Comment on Add a code to first and last line of all the subroutines in a perl package

Replies are listed 'Best First'.
Re: Add a code to first and last line of all the subroutines in a perl package
by Prof Vince (Friar) on Oct 23, 2007 at 07:51 UTC
Re: Add a code to first and last line of all the subroutines in a perl package
by merlyn (Sage) on Oct 23, 2007 at 05:49 UTC
    Would a trace from running it in a debugger suffice? You should be able to use a debugging hook to do what you want without altering any code.
      Thanks.

      Actually, a non perl tester is going to profile the code. I want them to do profiling in a painless way. The application is very big and Apache::SmallProf makes the application very slow.

      My idea is to add timestamp to begining and end of all subroutines for few packages.

        Read the documentation for Devel::SmallProf (Apache::SmallProf is a fork of it).

        These profilers can be configured to only profile certain packages. For instance, create a configuration file named .smallprof in your working directory like:

        %DB::packages = qw(Foo Bar);
        and only packages Foo and Bar will be profiled. But don't expect it to be much faster. Non-profiled code will still execute much slower (x2 - x5) than without the profiler.

        Probably you can also use $^P to explicitly activate/deactivate profiling (see perlvar and perldebguts).

        Finally, I would be very glad to collaborate with anyone trying to make Devel::FastProf work with apache (I'm its author!).

Re: Add a code to first and last line of all the subroutines in a perl package
by dragonchild (Archbishop) on Oct 23, 2007 at 12:50 UTC
    This is called Aspect-Oriented Programming. Aspect does all of this, as does Moose.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Add a code to first and last line of all the subroutines in a perl package
by eff_i_g (Curate) on Oct 23, 2007 at 15:48 UTC
Re: Add a code to first and last line of all the subroutines in a perl package
by clueless newbie (Curate) on Oct 23, 2007 at 12:22 UTC
    Have a look at London's Devel::AutoProfiler. He does essentially what you want but you'll need to modify some code.