Demonic_Dog
Demonic_Dog

Reputation: 142

How to load a bunch of perl modules into a perl script

This is what I wish to do:

In every script I make/develop I always call perl libraries and sub routines like:


#! /directory/bin/perl
system('source /directory/.cshrc&');

use Net::Domain qw(hostname hostfqdn hostdomain);
use Time::Local;
use Time::Piece;
use Switch;
use Exporter;
#use strict;

use Data::Dumper qw(Dumper);

use Time::Local;                                        
use Time::Piece; 
use Time::Seconds();                                       

use Tk;
use Tk::BrowseEntry; 
use Tk::Balloon;                                   
use Tk::widgets qw(Checkbutton BrowseEntry);            
use Tk::NoteBook;                                       
use Tk::Pane;                                           

use DBI;
use DBD::Oracle;
$ORACLE_HOME = "/lolDirectory/10.2.0/elinux";    
$ENV{ORACLE_HOME}=$ORACLE_HOME; 

###############
# SUBROUTINES #
###############

&ownerChecker;
&processChecker;    

I wish i can put all those to a file and load it to a perl script, in the same time running it as if it is part of the perl script itself like:


#! /directory/bin/perl

# load the content of the file and run it as a part of the script

Is this possible? If it is possible? If it is possible, from calling libs to calling checker scripts could be very generic and standard.

Upvotes: 3

Views: 365

Answers (1)

Schwern
Schwern

Reputation: 165416

Creating one module that loads other "standard" modules is the motivation for things like perl5i and Modern::Perl.

Pragma modules with lexical effect such as strict, warnings and autodie simply need to be loaded in your module's import routine. Modules which export functions need to be told to export their modules elsewhere, which can be done with Import::Into. Finally, classes simply need to be loaded.

Since use happens at compile time, you need to do the equivalent at runtime which is requireing the module and calling its import method.

Here's an example of turning on strict and warnings, loading Time::Local and loading Time::Piece, and activating the say and switch features.

package My::Perl;

use strict;
use warnings;
use Import::Into;

sub import {
    # import is called as a class method
    my $class = shift;

    # The class which 'use'd this module
    my $caller = caller;

    # same as "use strict" but happens when import() is called.
    require strict;
    "strict"->import;

    # use warnings;
    require warnings;
    "warnings"->import;

    # use Time::Local;
    # use Time::Piece;
    Time::Local->import::into($caller);
    Time::Piece->import::into($caller);

    # use feature qw(say switch);
    require feature;
    feature->import(qw(say switch));
}

1;

Now you just have to load that one module.

use My::Perl;

say localtime->year;

Don't go too crazy, you want these to be generically useful. It's silly to load DBI and Tk if you're not going to use them. If you want to load a bunch of Tk modules, make a separate My::Tk module to do that. And I wouldn't have the modules execute any code for the same reason.

Upvotes: 9

Related Questions