Hi. I'm trying to write a tool which would enable me to automatically determine the modules which are used when a specific module is loaded.

So, if Module::A uses Module::B which in turn uses Module::C I'd like to end up with a data structure that lets me know that Module::A uses Module::B uses Module::C in that order when Module::A is loaded.

To do that I'm trying to play around with %INC; here's what I've got so far.
sub get_inc { my $module = shift; local %INC; eval { require $module }; return keys %INC }
This allows me to inspect my local copy of %INC after a module has been required so that I can see what modules have been loaded after the fact.

However, the data returned doesn't provide any clues as to the order in which the modules were loaded. So I only know that then end result of requiring Module::A is that Module::B and Module::C were loaded; but I can't tell if Module::A loaded Module::C directly or if Module::C was loaded by Module::B.

So, what I am hoping to do is replace my local %INC with a tied hash that would allow me to inspect the contents of %INC as the modules are being 'required'.

Here are my questions:
  • Is it possible to manipulate %INC in this manner?
  • If not, is there another way to accomplish this?
  • I would like to avoid any solutions which involve the scanning of perl module files; at this point I'm hoping that the perl interpreter itself provides enough hooks to extract this information once a module has been required.

    In reply to Module Loading Tool by Anonymous Monk

    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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.