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

Hello, I want to create a common Perl module file that will have all the modules includes needed for my main perl script. how to do that.. Here is what i want to achieve. #abc.pm -----
package abc; use strict; use File::Path 'mkpath'; # for mkdir #use warnings; use integer; use File::Basename; use File::Copy; # for move use IO::Zlib; # to read gzip input files use IO::File; # to read non gzip input files use Text::Trim; use Data::Types qw(is_int is_string); use Shell qw (cat); use Sys::Hostname; use Time::Local;
....... so on i need to add some of my custom perl modules as well here. These modules are common that are being used for my different perl scripts. I want not to include all these on all perl scripts. just include this abc.pm into my perl scripts and all modules that are defined into abc.pm should be included on my perl script. like:
#myscript.pl use abc; ## i just need to include abc.pm and can use the module features that +are defined into abc.pm.
help needed. Thanks.

Replies are listed 'Best First'.
Re: Want to create a common Perl module
by alexbio (Monk) on Jun 28, 2010 at 13:54 UTC

    Plase use the <c> tags next time (Markup in the Monastery), as follows:

    #abc.pm ----- package abc; use strict; use File::Path 'mkpath'; # for mkdir #use warnings; use integer; use File::Basename; use File::Copy; # for move use IO::Zlib; # to read gzip input files use IO::File; # to read non gzip input files use Text::Trim; use Data::Types qw(is_int is_string); use Shell qw (cat); use Sys::Hostname; use Time::Local;

    This makes your code much more readable.

    Alex's Log - http://alexlog.co.cc

      Note: you have to end the module with something that evaluate to 'true'...the usual practice is to make 1; the last line.

      ack Albuquerque, NM
Re: Want to create a common Perl module
by almut (Canon) on Jun 28, 2010 at 14:13 UTC

    Something like this might work in principle:

    package abc; use strict; use warnings; use File::Path (); ... sub import { my $caller = caller(); strict->import(); warnings->import(); { eval "package $caller; File::Path->import('mkpath');" } ... } 1;

    One of the problems is that Exporter, from which many modules inherit the import() method, uses caller to determine where to export to... That's the reason for the eval, because the calling package that you have to mimic for this in abc's import() will only be known at runtime.

      I just tested. it is not working. gives error "Undefined subroutine &main::InitializeLog called at .....".
      I also tried a module named ToolSet for the same. but when i run it gives me error.
      "Base class package "ToolSet" is empty. (Perhaps you need to 'use' the module which defines that package first.)".

        Could you show the exact code that you've written for abc.pm?  Which module is InitializeLog supposed to be imported from?

Re: Want to create a common Perl module
by metaperl (Curate) on Jun 28, 2010 at 14:44 UTC

      It's maybe worth noting that this module uses source filtering, i.e. it on-the-fly edits the respective use statements into your source before it's being compiled.

      While this is kind of the easy way out for all potential namespace related issues with importing/exporting, it might cause other unexpected behavior on rare occasions (like any source filter).