Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Was a module use'd or require'd?

by Anno (Deacon)
on Aug 13, 2007 at 12:09 UTC ( [id://632189]=note: print w/replies, xml ) Need Help??


in reply to Was a module use'd or require'd?

One idea would be to make a snapshot of %INC when compile time ends. If an entry is in the snapshot, the module was use()d, otherwise it was require()d.

For instance:

CHECK { my %snap = %INC; sub report { for ( keys %INC ) { my $ans = $snap{ $_} ? 'use()d' : 'require()d'; print "$_ was $ans\n"; } } }
Here, report() simply prints a list with each module's status. A more elaborate version could return the status of a module that is passed in.

Of course, %INC can be manipulated in every which way, so the method isn't fool-proof. It makes up for that with simplicity.

Update:
Another possible drawback of this approach is that the module that implements it must itself be use()d and can't be loaded at run time. (It would be too late to run the CHECK block.) That may be true for other solutions also.

Anno

Replies are listed 'Best First'.
Re^2: Was a module use'd or require'd? (CHECK?)
by tye (Sage) on Aug 13, 2007 at 14:52 UTC

    I haven't tried to lay it out clearly, but it seems to me that this node works under the flawed but common (it seems) assumption that there is some one "compile time" followed by some one "run time". Each line of code has one "compile time" and zero or more "run times" and those intermix quite a bit in even simple cases. The CHECK block of one module seems unlikely to be reliably run between the "compile time" and "run time" of some other module.

    Without checking (since "perldoc -f CHECK" and other similar attemps to easily find the relevent documentation are likely all still useless), your use makes me guess that CHECK blocks are the ones that get run after the main script has finished compiling. So this would only be useful for distinguishing mundane use vs require from the main script.

    So I don't think that addresses the requested problem at all, which is trying to reconstruct whether Some::Module should be reconstructed as having used or as having required each of the modules it loaded.

    - tye        

      I think you are right. My method will tell whether a module was loaded before or after the main program was compiled, not whether it was a "use" or "require" statement that did the loading. It would be useless for the given purpose (about which I wans't quite clear when I wrote the reply).

      Putting a subref on @INC looks like the best bet. It should be possible to tell the cases from another by looking at some levels of caller(). One problem here is to make sure you are always called. If someone unshifts new components onto @INC in front of you, you may never be for some modules.

      Update: tie-ing @INC could help. It isn't magical, so tie should be no problem.

      Anno

Log In?
Username:
Password:

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

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

    No recent polls found