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

Hi Monks,

A pretty basic question. How do i override a package module, with my own package module ? I would like some switches in the command line itself, which could prioritise my module over the other. Can that be done ?

Thanks in advance..

Replies are listed 'Best First'.
Re: Overriding perl modules
by Corion (Patriarch) on Feb 21, 2011 at 18:18 UTC

    Just load your module before the other, and tell Perl not to load the other module. See perlrun for how to load a module from the command line, and perlvar on %INC (together with require) for how to tell Perl not to load a file.

Re: Overriding perl modules
by toolic (Bishop) on Feb 21, 2011 at 19:13 UTC
    Here are a few ways to select a different module from the command line. Let's say I want to modify the behavior of Carp, and I copied the Carp.pm file to my local /home/toolic/lib directory. These command lines will effectively prepend my local directory to the @INC array, and choose my Carp over the standard module:
    • perl -I/home/toolic/lib my_script.pl
    • perl -Mlib=/home/toolic/lib my_script.pl
    • env PERL5LIB=/home/toolic/lib my_script.pl
    See also perlrun and env
Re: Overriding perl modules
by cdarke (Prior) on Feb 21, 2011 at 20:03 UTC
    perl already has the -M command-line option, just use that to load the module you want. See perlrun.

    If that is too simplistic, then an easy way is to use the if pragma. You can test anything here provided it is available at compile time - which the command-line is. For example:
    use strict; use warnings; use if (defined $ARGV[0] && $ARGV[0] eq "angshuman"), 'Gash'; use if (@ARGV == 0), 'File::Basename'; # Now use File::Basename or Gash subroutine calls my $file = basename($0); print "$file\n";
Re: Overriding perl modules
by thargas (Deacon) on Feb 21, 2011 at 18:37 UTC

    If you mean that you want to have your version of Some::Module used instead of the system-installed version of Some::Module, then you'll need to manipulate @INC (probably using something like use lib "/some/dir/lib/perl5". Or you could require "/some/file.pm" which defines Some::Module before anything else does use Some::Module. Hooking that into your command-line will have to be done at compile-time (i.e. in a BEGIN block, but is otherwise straightforward.

    If you mean something else, you'll have to elaborate what you're trying to accomplish.

Re: Overriding perl modules
by Sherm (Sexton) on Feb 21, 2011 at 19:16 UTC
    The -I option to perl will prepend the supplied path to @INC, so any modules found there will be loaded in preference to those in the standard locations. See perlrun for details.