.pm file that's loaded on every invocation of the perl interpreter?

佐手、 提交于 2021-02-11 07:38:59

问题


I thought I remember reading somewhere about where perl can be configured to automatically load a certain .pm file on start up.

I know about PERL5OPT, but to my recollection, this was a specific file that would be loaded if it exists.

Is it a compile option that can be set (i.e. via Configure)?


回答1:


Reading through perldoc perlrun it looks like you are looking for what is talked about in the -f option:

-f

Disable executing $Config{sitelib}/sitecustomize.pl at startup.

Perl can be built so that it by default will try to execute $Config{sitelib}/sitecustomize.pl at startup (in a BEGIN block). This is a hook that allows the sysadmin to customize how Perl behaves. It can for instance be used to add entries to the @INC array to make Perl find modules in non-standard locations.

Perl actually inserts the following code:

BEGIN {
do { local $!; -f "$Config{sitelib}/sitecustomize.pl"; }
&& do "$Config{sitelib}/sitecustomize.pl";
}

Since it is an actual do (not a require), sitecustomize.pl doesn't need to return a true value. The code is run in package main , in its own lexical scope. However, if the script dies, $@ will not be set.

The value of $Config{sitelib} is also determined in C code and not read from Config.pm , which is not loaded.

The code is executed very early. For example, any changes made to @INC will show up in the output of perl -V. Of course, END blocks will be likewise executed very late.

To determine at runtime if this capability has been compiled in your perl, you can check the value of $Config{usesitecustomize} .

I've never done this, but it looks like if you put what you want in $Config{sitelib}/sitecustomize.pl you'll get what you are looking for.

See:

  • http://perldoc.perl.org/perlrun.html
  • http://www.nntp.perl.org/group/perl.perl5.porters/2007/10/msg129926.html



回答2:


I'm confused by what you mean by "on start up". If you mean when a script / CGI / whatever is "started", then just use the module in the script:

use Data::Dumper;

Or do you mean something else?



来源:https://stackoverflow.com/questions/7051971/pm-file-thats-loaded-on-every-invocation-of-the-perl-interpreter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!