6

The perl scripts contain all the module names in the beginning of the script.

Ex:

use strict;
use XML::Parser;
use XML::Simple;
use DBI;
use DBD::DB2::Constants;
use POSIX qw( strftime );
use Storable qw(dclone);
use Data::Dumper;
use Carp;

How to keep all the module names in another file and include the file in main perl script ?

Thanks.

Amir E. Aharoni
  • 1,308
  • 2
  • 13
  • 25
Sourcecode
  • 305
  • 4
  • 13
  • Previously: http://stackoverflow.com/questions/437785/is-there-a-way-to-use-a-single-file-that-in-turn-uses-multiple-others-in-perl http://stackoverflow.com/questions/4919625/perl-how-to-share-the-import-of-a-large-list-of-modules-between-many-independen http://stackoverflow.com/questions/6412799/perl-how-to-make-use-mydefaults-with-modern-perl-utf8-defaults – daxim Nov 19 '12 at 19:48

3 Answers3

7

You can use

BEGIN { do 'filename.pl' or die $@ }

See do. Be sure to include 1; at the bottom of the file.

ikegami
  • 367,544
  • 15
  • 269
  • 518
choroba
  • 231,213
  • 25
  • 204
  • 289
6

Create your own module. But, if if you make package in this module those strftime etc will be imported into another namespace. You can do litle hack to do this smiple:

MyModules.pm:

use strict;
use XML::Parser;
use XML::Simple;
use DBI;
use DBD::DB2::Constants;
use POSIX qw( strftime );
use Storable qw(dclone);
use Data::Dumper;
use Carp;

Note there was no package keyword in this .pm. Your script:

use MyModules;

Place this .pm into same dir with script or add modules search path at runtime:

use lib '/my_modules_dir/';
use MyModules;

Use of do and require commands is not recommended, since they do not check syntax on script start. Sure you can place them into BEGIN block but i think its tricky way and generally BEGIN{ require ..} is the same as use

UPD: ikegami noted problem its not working when you use this from many modules. Regardless question starter's information (he want to use this from main script), ill add info how to use this from many modules, so my friend can feel better. Add to end of MyModules.pm:

delete $INC{'MyModules.pm'};
1;
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • 1
    Actually `use` when file doesnot contain package ends with `do` in `BEGIN` anyway. Difference that `use` will do the checks for you. And, as in case with choroba's answer you will edit comment/catch the bugs with such simple thing like `do` command. – Galimov Albert Nov 15 '12 at 07:59
  • @ikegami yes, **use** will result **require** which itself will call **do** after checking that module isnot loaded already. And he wants to include from main script. – Galimov Albert Nov 15 '12 at 17:36
  • @ikegami everything for you, my best stackoverflow friend. – Galimov Albert Nov 15 '12 at 17:57
  • .@ PSIAlt: Thanks a lot. Why to use the statement. delete $INC{'MyModules.pm'}; – Sourcecode Nov 20 '12 at 15:20
  • @Sourcecode generally cus its a hack. But ironically Perl is 1/4 made of hacks xD – Galimov Albert Nov 20 '12 at 16:55
3

You might look at the Toolkit module.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97