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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I made some improvements on the program, so it will handle the command line arguments and it will show the parent filename at each node. And there is no need to generate a PS image.

#!/opt/csw/bin/perl - # Author : Rob G, The Netherlands # Based on script by: Valter Mazzola, txian@hotmail.com, Ital +y # Date : 24/May/2012 # Purpose: # ---> Generate a Graph-ical call tree for your *.pm perl module files +. # gra.pl assumes that: # 1- you have defined sub(s) with 'sub myfunc {' with 'sub' at the be +ginning of line. # 2- you call your sub with the '&' and brackets, i.e. &my_sub (); # usage: # 1) generate the .dot text graph file definition # perl gra.pl *.pm > myfile.dot # 2) generate the graph using 'dot' executable ( http://www.research.a +tt.com/sw/tools/graphviz/ ) # dot -Tjpg myfile.dot -o myfile.jpg # 3) display the graph # JPG viewer use File::DosGlob; my $fc = 1; # a file counter to distingguish between multiple main-fil +es # first expand the command line arguments @ARGV = map { my @g = File::DosGlob::glob($_) if /[*?]/; @g ? @g : $_; } @ARGV; # then loop through all files and exclude myself from the loop for my $file (@ARGV) { if ($file ne $0) { open(my $fh, $file) or die "Can't open $file: $!\n"; $cur_sub = "main".$fc; # used to show the parent of main subs $fc++; while (<$fh>) { if (/^sub\s+(.*?)\s*\{/) { $cur_sub = $1; $modules{$cur_sub} = $file; } if (/\&([\d\w_]+)\s*\(/){ $c_sub = $1; $n = 0; foreach $k (@{$called_subs{$cur_sub}}) { if ($c_sub eq $k) { $n = 1; last; } } if ($n == 0) { push @{$called_subs{$cur_sub}}, $c_sub; $modules{$cur_sub} = $file; } } } close $fh; } } print "digraph G {\n"; print " page=\"44,68\";\n"; # make sure the gr +aph is large enough print " ratio=auto;\n"; print " rankdir=LR;\n"; # ) I prefer porta +it print " orientation=portrait;\n"; # ) print " node[fontsize=10,fontname=\"Arial\"]\n"; # and another font +name, fontsize # first we define the label and shape of each node # the label is contructed from the name of the sub and the name of the + parent file # the shape for the main entries is different from the other nodes # # a side-effect: it will show all sub's that are not used too while (($key, $value) = each (%modules)) { if (substr($key, 0, 4) eq "main") { print " $key [shape=ellipse,label=\"main\\n($value)\"];\n"; } else { print " $key [shape=box,label=\"$key\\n($value)\"];\n"; } } # then we define the paths between the nodes foreach $k (keys(%called_subs)) { $ref_arr = $called_subs{$k}; if (ref($ref_arr)) { foreach $y (@{$ref_arr}){ print " $k -> $y;\n"; } } } print "}\n";

In reply to Re: Generate a Graph-ical call tree for your *.pm perl modules by RobG
in thread Generate a Graph-ical call tree for your *.pm perl modules by trony

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found