Perl logs which scripts / modules access another module

We support a huge number of perl modules, so huge that we do not even know about all the modules for which we are responsible. We would like to keep track of which scripts and modules are accessing another module in some kind of log, preferably stored by the name of the module, so that we can evaluate whether it can update the module and so that we can know what we can influence.

Is there an easy way to do this?

+4
source share
2 answers

Perhaps edit the sitecustomize.pl file so that every time Perl launches, it writes some information to the log and then analyzes it? Add something like sitecustomize.pl:

open (LOG, '>>',"absolutepathto/logfile.txt"); print LOG $0,"\t",$$,"\t",scalar(localtime),"\n"; open SELF, $0; while (<SELF>) { print LOG $_ if (/use|require/); } close SELF; print LOG "_" x 80,"\n"; close LOG; 

EDIT: In addition, we forgot about the% INC hashes, so the code above can be rewritten as follows to add more data about which modules were actually loaded + include files needed for the do function:

 open (LOG, '>>',"absolutepathto/logfile.txt"); print LOG $0,' ',$$,' ',scalar(localtime),"\n"; open SELF, $0; while (<SELF>) { print LOG $_ if (/use|require/); } close SELF; END { local $" = "\n"; print LOG "Files loaded by use, eval, or do functions at the end of this program run:\n"; print LOG "@{[values %INC]}","\n"; print LOG "_" x 80,"\n"; close LOG; } 
+1
source

You can do a simple regular expression search:

 use strict; use warnings; my %modules; foreach my $perl_file (@file_list) { open FILE, $perl_file or die "Can't open $perl_file ($!)"; while (<FILE>) { if (/\s*(?:use|require)\s*([^;]+);/) { $modules{$1}{$perl_file}++; } } } 

It is fast and dirty, but should work very well. You get a hash of modules, each of which points to a hash of the files that use it.

Of course, he will catch things like use strict; but they will be easy enough to ignore.

If you have things like use Module qw/function/; You will grab it all to the semicolon, but this should not be a big problem. You can simply search for keys for your well-known module names.

The disadvantage is that it does not track dependencies. If you need this information, you can add it by getting it from cpan or something like that.

Update:. If you want to register this at run time, you can create a shell script and point your perl command to your system shell. Then make a shell something like this:

 use strict; use warnings; use Module::Loaded; my $script = shift @ARGV; #run program do $script; #is_loaded() gets the path of these modules if they are loaded. print is_loaded('Some::Module'); print is_loaded('Another::Module'); 

You might be risking ridiculous side effects as the method of invoking your script has changed. It depends on what you do.

+3
source

Source: https://habr.com/ru/post/1441997/


All Articles