Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

wishlist: static analysis of perl modules

by dash2 (Hermit)
on Feb 23, 2003 at 16:32 UTC ( [id://237905]=perlquestion: print w/replies, xml ) Need Help??

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

I've been looking for a few development tools and I can't seem to find them, but I strongly suspect they are there. Most relate to the static structure of perl modules - e.g. not "how often does this get called when you run the code?" but "how many places is this called from?"

Anyone got any CPAN ideas for modules which could...

    cheers!

  • ... edit my code and change names of a variable or subroutine, wherever it's referred to. (Obviously wouldn't catch stuff like eval '$' . 'foo';
  • ... find uncalled subroutines in my packages. (I'd forget my head if it was loose.)
  • ... build a pretty picture of which packages call which others. (UML?)
  • ... warn me about private subroutines (sub _foo) being called from outside their class/subclasses
  • ... do the ironing ...
  • ... increase my attractiveness to women ...

dave hj~

Replies are listed 'Best First'.
Re: wishlist: static analysis of perl modules
by valdez (Monsignor) on Feb 23, 2003 at 16:56 UTC
      Hi Valerio

      yes, I know about DProf, but I was looking for static analysis. DProf requires you to run the code, it then profiles it and you can look at e.g. which subroutines are slowest. It can also print out a subroutine call tree, however, I don't know that there are any ways of limiting this call tree to only show "my" code (not code from external modules I am using.)

      While I'm on the subject, one more wish I forgot: use everywhere; - a way to import stuff to all my namespaces. Useful for quick development, e.g. use everywhere 'Data::Dumper', qw/Dumper/;

      dave hj~

Re: wishlist: static analysis of perl modules
by tall_man (Parson) on Feb 23, 2003 at 17:03 UTC
    Finding out statically how many places a subroutine is called from would be an impossible task in perl. Think about routines called through object references with types that vary at runtime, eval blocks with variable substitutions, and new overloads that might be created at any time.

    I think your best bet would be detailed runtime tracing using caller, perhaps hooked in with Devel::AutoProfiler

      True. As I said, I wouldn't expect this code to understand stuff like evals. Just to get the vanilla stuff.

      Actually, the request for "static" wasn't because I really can't run the program. It was more to direct people's attention away from runtime characteristics of the code, and towards the structure of the code. For example, finding subroutines which never get called (I assume that trying to do this at runtime would involve us in the halting problem :-)

      dave hj~

        if you're rather consistant in your coding style (or maybe after a run through perltidy) and you're familiar with the way the code is constructed then it seems you could do some of what you want with simple text processing.

        just grep for /sub/ and build a list of your named subroutines, then grep for the subroutine names on lines that don't match /sub/.

        i've been planning to look at perltidy's source for ideas along the same lines as your question. i figure if perltidy can grok perl enough to colorize and format perl nicely then it might be easy enough to make it do other transformations on the code. i'd like to be able to do something like:

        package F; my %D = ( foo => 1 ); sub new { my ($c,%p) = @_; my %s = ( %D, %p ); return bless \%s, $c; } sub f_1 { my $s = shift; ... } package main; my $f = new F; print $f->f_1('blah');

        and then be able to generate a transform file.

        F -> Foo %D -> Default &new $c -> class %p -> parameter %s -> self &f_1 -> marble $s -> self main $f -> foo_obj

        and then be able to swap back and forth. most of the stuff i need to get done deals with tight name domains, and by that i mean that:

        j is a jack h is a host ac is an account t is a topology p is a port v is a vlan n is a netmask b is a building r is a room m is a MAC address

        it doesn't confuse me at all, might be because i started BASIC back in the early Apple II days when you could only use single character variables, and the days of Fortran where the initial character represented int/float, or maybe because i was a physic major and they'll go into foreign characters before they'll use longer varibles.

        but i realize that for the people who look at my code when i'm done just see noise unless the variables are longer.

        once the code is actually written i don't have a problem with the names being long, i just haven't found a better way as of yet than manually (vim, so it isn't that hard =)

        I wanted to second zengargoyle's comment above (++). If you restate the problem as finding subroutines which are never mentioned, as opposed to called, you could do various grep-type searches. Of course, Perl's complex syntax makes it difficult to identify even mentions of subroutines with real precision. If you have a sub called add, for example, you need to know that print "add" is not a subroutine call, but that print "@{[ add ]}" is.)

        As for edit my code and change names of a variable or subroutine, wherever it's referred to... well, I admit I typically use a low-tech solution in my editor called "search and replace" :-)

        To warn me about private subroutines (sub _foo) being called from outside their class/subclasses: Seems like you could insert a line at the beginning of the private sub that would check UNIVERSAL::isa to see if the calling object is a descendant of the appropriate type.

        And finally, increase my attractiveness to women. I am inclined to respond that "I have discovered a truly remarkable technique which this textbox is too small to contain." (With tongue firmly in cheek, and copious apologies to Pierre de Fermat.)

                $perlmonks{seattlejohn} = 'John Clyman';

Re: wishlist: static analysis of perl modules
by bsb (Priest) on Feb 23, 2003 at 22:21 UTC
    Maybe B::Xref and B::Graph could help you.
Re: wishlist: static analysis of perl modules
by John M. Dlugosz (Monsignor) on Feb 24, 2003 at 03:22 UTC
Changing var names
by czth (Sexton) on Feb 24, 2003 at 18:30 UTC
    The old replace-in-files standby
    perl -pi -e 's/\$oldname\b/\$oldname/g' <files>
    should take care of 99% of your variable replacement needs (or from vi,
    :%!perl -pe 's/\$oldname\b/\$oldname/g'
    should do the trick nicely).

    czth

Re: wishlist: static analysis of perl modules
by zemote (Scribe) on Feb 24, 2003 at 07:18 UTC
    Unfortunately there is only one thing that is gonna increase your attractiveness to women. Lot's of Money!!!!!!!!!!!! 1) Develop software that makes you lots of money. 2) You may want to read Bar'ts Dating guide for geaks. http://www.linuxhelp.ca/guides/geekguide/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://237905]
Approved by pfaut
Front-paged by abell
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found