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!
| [reply] [d/l] [select] |
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.
| [reply] |
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.
| [reply] [d/l] [select] |
|
Actually, it's much simpler than that:
END {
print STDERR "Modules used:\n", map { " $_\n" } values %INC;
}
| [reply] [d/l] |
|
| [reply] [d/l] |
|
Hi, Merlin.
I'm curious... Why printing to STDERR instead of STDOUT?
Regards,
| [reply] |
|
|
Brilliant. Can I suggest sorting for readability:
END {
print STDERR "Modules used:\n", map { " $_\n" } sort values %INC;
}
| [reply] [d/l] |
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
|
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
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
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
| [reply] |
|