Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Changing every subroutine in many perl scripts

by Jenda (Abbot)
on Jul 25, 2012 at 08:19 UTC ( #983581=note: print w/ replies, xml ) Need Help??


in reply to Changing every subroutine in many perl scripts

Do you need the text to be in the files or do you need to do something at the start of the subroutines' execution? If the later, have a look at Hook::LexWrap, Class::Wrap and possibly Devel::TraceSubs for an example how to wrap everything in a package.

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re: Changing every subroutine in many perl scripts
Re^2: Changing every subroutine in many perl scripts
by nitin1704 (Sexton) on Jul 26, 2012 at 03:36 UTC

    Okay, so I tried the following code:

    #!/usr/bin/perl use warnings; use strict; use Hook::LexWrap; my @mars_subs = qw (add increment); for (@mars_subs) { wrap $_, pre => sub { my @caller = caller(0); for (@caller) { print $_, " " if defined $_; } print "\n"; }; } print add( 1, 2 ), "\n"; sub add { my ( $num1, $num2 ) = @_; increment($num1); return $num1 + $num2; } sub increment { my $num = shift; return ++$num; }

    And the output I got is:

    main hooklexwrap.pl 17 main::__ANON__ 1 1 1794 UUUUUUUUUUUUU main hooklexwrap.pl 21 main::__ANON__ 1 1538 UUUUUUUUUUUUU 3

    How do I get the actual sub names like main::add instead of main::__ANON__ ?

      What are you trying to report? Are you trying to emit something for every executed function? In that case, close over the name of the wrapped function:

      my @mars_subs = qw( add increment ); for my $sub (@mars_subs) { wrap $sub, pre => sub { print "Calling '$sub'\n"; }; }

      Improve your skills with Modern Perl: the free book.

        Thanks. Yes, I want to log how these various subroutines are called one after the other, for understanding how this set of perl scripts and modules works and the dependencies between them. It's all legacy code of thousands of lines. Is there a better way to do this?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-10-22 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls