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

Is there a way to get perl to always load a module? I need to have a certain perl module loaded any time someone calls perl.

Replies are listed 'Best First'.
Re: Automatically load module on startup?
by moritz (Cardinal) on Mar 09, 2012 at 06:58 UTC
Re: Automatically load module on startup?
by Anonymous Monk on Mar 09, 2012 at 04:15 UTC

      Yay sitecustomize.pl, no environment variable tweakage needed.

      $ cat /var/local/perl/5.14.2/lib/site_perl/5.14.2/sitecustomize.pl package AlwaysRun; use Carp qw(:DEFAULT cluck); cluck "$0 $$: I am begun at " . localtime(); 1; $ perl -E 'say "howdy"' -e 16199: I am begun at Fri Mar 9 00:16:58 2012 at /var/local/perl/5. +14.2/lib/site_perl/5.14.2/sitecustomize.pl line 3. require /var/local/perl/5.14.2/lib/site_perl/5.14.2/sitecustom +ize.pl called at -e line 0 main::BEGIN() called at /var/local/perl/5.14.2/lib/site_perl/5 +.14.2/sitecustomize.pl line 0 eval {...} called at /var/local/perl/5.14.2/lib/site_perl/5.14 +.2/sitecustomize.pl line 0 howdy
      Thanks, that is exactly what I needed. I was just looking at that documentation yesterday, but must have glanced over it.
Re: Automatically load module on startup?
by tchrist (Pilgrim) on Mar 09, 2012 at 05:57 UTC
    This is fragile, but you get the idea:
    $ cat -n AlwaysRun.pm 1 package AlwaysRun; 2 3 use Carp qw(:DEFAULT cluck); 4 5 cluck "$0 $$: I am begun at " . localtime(); 6 7 1; $ export PERL5OPT=-MAlwaysRun $ perl -E 'say "howdy"' -e 4574: I am begun at Thu Mar 8 22:55:52 2012 at AlwaysRun.pm line 5 require AlwaysRun.pm called at -e line 0 main::BEGIN() called at AlwaysRun.pm line 0 eval {...} called at AlwaysRun.pm line 0 howdy
Re: Automatically load module on startup?
by bms (Monk) on Mar 09, 2012 at 04:30 UTC

    Without knowing your exact requirements, you may want to take a look at the Autoloader module. If you need certain functions available for one one liners from an assortment of modules that you only need a subroutine or two from, AutoLoader may be a good option. Although this is a bit of work, and requires writing .al files.

    If you're interested in AutoLoader, you may also want to check out this short guide.

      I haven't used AutoLoader in ages. How would you use it to load a module every time someone invokes perl?

        AutoLoader doesn't load modules. If perl receives an undefined subroutine, it starts looking in the auto directory for it. If it finds the proper .al file, it uses it.

        Despite AutoLoader not loading full modules, it may be something that the OP may want to look at.