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

I have a module that I've created called testmodule.pm and I have it placed in a folder on my desktop. Is there a way to get Perl to find this module without including it in the @INC? Or do I have to include the path to this module in the @INC?
  • Comment on how to get a module to work in a specific directory

Replies are listed 'Best First'.
Re: how to get a module to work in a specific directory
by FunkyMonk (Bishop) on Jul 20, 2008 at 22:57 UTC
    Can't you just use

    use lib '/directory/containing/your/pm'; use testmodule;

    or

    BEGIN { require 'path/to/testmodule.pm'; your::package::name->import; }

    see lib or use for more information


    Unless I state otherwise, all my code runs with strict and warnings
Re: how to get a module to work in a specific directory
by shmem (Chancellor) on Jul 20, 2008 at 22:42 UTC

    Either way is fine. You can include the path of the module's directory in @INC or in the environment variable PERL5LIB (see perlrun). Or you can change directory in a BEGIN block prior to using the module:

    BEGIN {chdir "/path/to/Desktop"}
    since the current working directory (".") is in perl's search path.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}