Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Is there a module which Perl has to load when using any module?

by zentara (Archbishop)
on Apr 28, 2005 at 13:07 UTC ( [id://452289]=perlquestion: print w/replies, xml ) Need Help??

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

This is a question prompted by What modules are we actually using?. I was wondering if there is some core Perl module which has to be loaded, when it loads another module thru use or require? My idea would be to temporarily modify that module, to print to a log file.

UPDATE ( 1 day later ) I did figure out how to use a custom module, loaded with PERL5OPT, to do this. See my reply below.


I'm not really a human, but I play one on earth. flash japh
  • Comment on Is there a module which Perl has to load when using any module?

Replies are listed 'Best First'.
Re: Is there a module which Perl has to load when using any module?
by ihb (Deacon) on Apr 28, 2005 at 13:16 UTC

    No, there isn't. However, require makes use of @INC and by unshifting a code ref into it you can do the same thing.

    ihb

    See perltoc if you don't know which perldoc to read!

Re: Is there a module which Perl has to load when using any module?
by adrianh (Chancellor) on Apr 28, 2005 at 13:19 UTC
    I was wondering if there is some core Perl module which has to be loaded, when it loads another module thru use or require?

    Nope, but you could get a similar affect by adding an appropriate coderef to @INC. See the documentation on require for details.

Re: Is there a module which Perl has to load when using any module?
by blazar (Canon) on Apr 28, 2005 at 13:16 UTC
    This is a question prompted by What modules are we actually using?. I was wondering if there is some core Perl module which has to be loaded, when it loads another module thru use or require? My idea would be to temporarily modify that module, to print to a log file.
    What does that "it" refer to? Perl? If so, then I don't think so. Otherwise, unless I'm severely mistaken, I don't think so either.

    However if all you want is to {know,log} which modules get loaded, then you can easily spot the possibility to put code into @INC to do so. E.g.:

    #!/usr/bin/perl -l use strict; use warnings; END { my @stuff; sub record { push @stuff, $_[1]; undef; } print for @stuff; } use lib \&record; use File::Find; no lib \&record; # Do something else, I suppose! __END__
    For the record this had come up in clpmisc and this kind of solution was suggested by Anno Siegel, who is a precious contributor there. As time permits I'll try to locate the original post.
        Indeed. Of course the original discussion was about how to monitor module loading on a "per use statement basis", as can be suggested by the minimal example supplied - still sloppily speaking here, but I guess it's easy to understand what I really mean here.

        But then again it was just an example of how to use a coderef in @INC to achieve something similar to what the OP wanted...

        Brilliant. Can I suggest sorting for readability:
        END { print STDERR "Modules used:\n", map { " $_\n" } sort values %INC; }
Re: Is there a module which Perl has to load when using any module?
by zentara (Archbishop) on Apr 29, 2005 at 10:40 UTC
    Thanks for all the replies. I did figure out something...just create my own module, and automatically load it with PERL5OPT.

    In your bashrc (or whichever) put

    export PERL5OPT=-MLogger
    Then put this Logger.pm in your PERL5LIB
    package Logger; use strict; use warnings; print "Logger\n"; 1;
    Of course this is just a rudimentary test. You could put whatever code you wanted in there....something to append to a logfile. I wonder if you would need to flock it, if a bunch of perl scripts were running constantly.

    I'm not really a human, but I play one on earth. flash japh

      I don't understand how this would work. How does use/require interact with Logger?

      ihb

      See perltoc if you don't know which perldoc to read!

        I havn't experimented further with it, but what it does, is allow you to log each Perl script that runs, without any changes to each script. You could then just filter the list of Perl scripts from the log, and search thru them for "use and require statements". That would be an easy way of telling which modules are used on your setup.

        When I have the inclination, I want to experiment around with putting END{} blocks in the Logger module(as Merlyn suggests for printing actual modules loaded, and see if it can be exported to main automatically.(I'm only a module novice, so I may be on a dead-end street here)

        The idea is to monitor a system, running for a few weeks, to see which modules are used, or even "used the most".


        I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found