Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to keep track of INC hash?

by jacques (Priest)
on Jul 15, 2008 at 04:32 UTC ( [id://697633]=perlquestion: print w/replies, xml ) Need Help??

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

Please read carefully. This question is tricky.
At the beginning of my module I examine %INC with a BEGIN block, like so:
BEGIN { %Seen = %INC }
That tells me what %INC looks like before I 'use' anything in my module. In other words, it tells me what files, if any, were loaded before my own.

What I would like to know are the entries made to %INC by my module and *only* by my module. So if my module doesn't use CGI.pm and a script is using my module like so:

use My::Module; use CGI;
I could tell that the CGI module was used by the script and not by my module. How could I figure this out from within my module? I tried using an END block to no avail.

Replies are listed 'Best First'.
Re: How to keep track of INC hash?
by grinder (Bishop) on Jul 15, 2008 at 05:27 UTC

    What is preventing you from installing a handler in @INC?

    perl -le 'BEGIN{unshift @INC, sub {print $_[1]}} use CGI; # ... produces CGI.pm Carp.pm Exporter.pm CGI/Util.pm strict.pm vars.pm warnings/register.pm warnings.pm constant.pm overload.pm

    It is then a simple matter of programming to figure out if your module is the one doing the loading. Or maybe not, if that's why you're asking the question. Is there something that rules out this approach?

    • another intruder with the mooring in the heart of the Perl

      Worked for me, too... I did:
      $ perl -e ' BEGIN { open my $f, "|sort>/tmp/uses.txt"; unshift @INC, sub { push @_, caller; print $f "module $_[2] uses $_[1]\n" } } do "bin/ponto" '
      where bin/ponto is the name of some script of mine, and I got the following in /tmp/uses.txt:
      module base uses vars.pm module Carp uses Exporter.pm module Compress::Zlib uses Compress/Raw/Zlib.pm module Compress::Zlib uses IO/Compress/Base/Common.pm
      ...
      module main uses bin/ponto module main uses Date/Manip.pm module main uses locale.pm module main uses open.pm module main uses strict.pm module main uses utf8.pm module main uses warnings.pm module main uses WWW/Mechanize.pm module main uses YAML/Syck.pm
      ...
      []s, HTH, Massa
      ++ Your response answered my question as posed. Upon further reflection, I realized that what I really want is to prevent my module from making additions to INC in the first place. Is there a way to do that? Basically what I want to find out is the contents of the INC hash from a script that uses my module (but my module's dependencies should not be included in that data, unless those dependencies were used elsewhere in the program). Perhaps I could delete those particular additions to INC in an END block?
        I really want is to prevent my module from making additions to INC in the first place

        In other words, you don't want your module to load additional packages? In that case, return undef from your filter. See require for more information (I think this is the crux of matter in the wisdom you seek).

        • another intruder with the mooring in the heart of the Perl

Re: How to keep track of INC hash?
by GrandFather (Saint) on Jul 15, 2008 at 04:46 UTC

    Noname1.pm:

    package Noname1; use strict; use Warnings; my @added; BEGIN { my %Seen = %INC; require CGI; @added = grep {! exists $Seen{$_}} keys %INC; } sub Added { return @added; } 1;

    Noname.pl:

    use strict; use Warnings; use Noname1; use CGI; my @added = Noname1::Added (); print "@added";

    Prints:

    CGI/Util.pm CGI.pm

    Perl is environmentally friendly - it saves trees

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found