Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
    0: # (Please move this node if it belongs in Snippets or something)
    1: #
    2: # This little program will print a prettified inheritance
    3: # tree for the given perl module.  Its usage is:
    4: # perl-inheritance [<options>] <module-name>
    5: # e.g.: perl-inheritance Class::DBI
    6: #
    7: # Available options are:
    8: # -I<path> : include <path> in @INC
    9: # -a : attempt to use all modules instead of just the root one
    10: # -i : ignore modules that can't be found
    11: #
    12: # Some example output:
    13: # perl-inheritance Class::DBI
    14: # Class::DBI (v0.93)
    15: # +---Class::DBI::__::Base (v-1, set by base.pm)
    16: #     +---Class::Data::Inheritable (v0.02)
    17: #     +---Class::Accessor (v0.18)
    18: #     +---Ima::DBI (v0.29)
    19: #         +---Class::WhiteHole (v0.04)
    20: #         +---DBI (v1.37)
    21: #         |   +---Exporter (v5.567)
    22: #         |   +---DynaLoader (v1.04)
    23: #         +---Class::Data::Inheritable (loaded by Class::DBI::__::Base)
    24: #
    25: # perl-inheritance Net::FTP
    26: # Net::FTP (v2.71)
    27: # +---Exporter (v5.567)
    28: # +---Net::Cmd (v2.24)
    29: # |   +---Exporter (loaded by Net::FTP)
    30: # +---IO::Socket::INET (v1.26)
    31: #     +---IO::Socket (v1.27)
    32: #         +---IO::Handle (v1.21)
    33: #             +---Exporter (loaded by Net::FTP)
    34: 
    35: #!/usr/local/bin/perl -w
    36: use warnings;
    37: use strict;
    38: no strict 'refs';
    39: 
    40: my @ignore_list      = ();
    41: my $ignore_not_found = 0;
    42: my %already_loaded   = ();
    43: my $load_all         = 0;
    44: 
    45: ARG: while (@ARGV) {
    46:   SWITCH: {
    47:     ($ARGV[0] =~ /\-I(.+)/o) && do {
    48:       eval "use lib '$1';";
    49:       shift @ARGV;
    50:      last SWITCH;
    51:     };
    52:     ($ARGV[0] =~ /\-i$/o) && do {
    53:       $ignore_not_found = 1;
    54:       shift @ARGV;
    55:      last SWITCH;
    56:     };
    57:     ($ARGV[0] =~ /\-a$/o) && do {
    58:       $load_all = 1;
    59:       shift @ARGV;
    60:      last SWITCH;
    61:     };
    62:     ($ARGV[0] =~ /\-i=(.+)/o) && do {
    63:       @ignore_list = split " ", $1;
    64:       shift @ARGV;
    65:      last SWITCH;
    66:     };
    67:    last ARG;
    68:   } ## end SWITCH:
    69: } ## end while (@ARGV)
    70: 
    71: if (!@ARGV) {
    72:   print STDERR "Usage: $0 <perl modules>\n";
    73:   exit 1;
    74: }
    75: 
    76: foreach (@ARGV) {
    77:   %already_loaded = ();
    78:   ScanModule(undef, $_, 0);
    79: }
    80: 
    81: sub ScanModule {
    82:   my $parent  = shift;
    83:   my $module  = shift;
    84:   my $depth   = shift;
    85:   my @total   = @_;
    86:   my $ignored = 0;
    87:   my $loaded  = 0;
    88: 
    89:   $loaded = 1 if (exists $already_loaded{$module});
    90: 
    91:   eval "use $module" if (!defined $parent || $load_all);
    92:   if ($@ =~ /Can't locate .+ in \@INC/o) {
    93:     if ($ignore_not_found
    94:       || index("@ignore_list ", "$module ") != -1) {
    95:       $ignored = 1;
    96:       } else {
    97:       die "Error using $module: $@\n";
    98:     }
    99:   } elsif ($@) {
    100:     die "Error using $module: $@\n";
    101:   }
    102: 
    103:   if ($depth > 1) {
    104:     for (my $iter = 0; $iter < @total - 2; $iter += 2) {
    105:       if ($total[$iter] < $total[$iter + 1]) {
    106:         print "|   ";
    107:       } else {
    108:         print "    ";
    109:       }
    110:     } ## end for (my $iter = 0; $iter...
    111:   } ## end if ($depth > 1)
    112: 
    113:   if ($depth > 0) {
    114:     print "+---";
    115:   }
    116: 
    117:   print $module;
    118:   print " (ignored)" if ($ignored);
    119:   if ($loaded) {
    120:     print " (loaded by $already_loaded{$module})\n";
    121:   } else {
    122:     my $version = $module->VERSION();
    123:     print " (v$version)" if $version;
    124:     print "\n";
    125:     my $isa   = "${module}::ISA";
    126:     my $count = 1;
    127:     my $total = @$isa;
    128: 
    129:     foreach (@$isa) {
    130:       ScanModule($module, $_, $depth + 1, @total, $count, $total);
    131:       $count++;
    132:     }
    133:     $already_loaded{$module} = $parent;
    134:   } ## end else [ if ($loaded)
    135: } ## end sub ScanModule
    

In reply to Prettified Perl Inheritance by Kageneko

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2015-07-30 23:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (273 votes), past polls