Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: how to share the import of a large list of modules between many independent scripts?

Tags:

module

perl

I have many standalone scripts. The only thing they share, is that they use() a large set of CPAN modules (that each export several functions). I would like to centralize this list of modules. I found several methods. Which one is the best?

  1. I could create SharedModules.pm that imports everything and then manually exports everything to main:: using Exporter.

  2. I could create SharedModules.pm that starts with "package main;" so it will import directly into main::. It seems to work. Is it bad practice and why?

  3. I could require() a sharedmodules.pl that seems to import everything into main:: as well. I don't like this method as require() doesn't work that well under mod_perl.

Number two looks best to me, however I wonder why for example Modern::Perl doesn't work that way.

Edit: I figured this question has been asked before.

like image 729
Willem Avatar asked Feb 07 '11 09:02

Willem


1 Answers

Maybe more flexible than putting everything into main namespace would be to import into caller's namespace. Something like this:

package SharedModules;

sub import {

    my $pkg = (caller())[0];
    eval <<"EOD";

package $pkg;

use List::Util;
use List::MoreUtils;

EOD

    die $@ if $@;
}

1;
like image 191
bvr Avatar answered Oct 10 '22 13:10

bvr