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

Dear Masters,
Suppose I have a code (mycode.pl) which uses my self created package (MyPackage.pm), and this package is stored under subdirectory ./modules_src/MyPackage.pm. My question is how can I appropriately call MyPackage.pm from mycode.pl?

I tried this but doesn't seem to work:
#!/usr/bin/perl -w # mycode.pl use 'modules_src/MyPackage'; # how can do this correctly? my $start = new Sth::MyPackage; # etc...
The overall code directory structure look simply like this:
~/MyPerl | |_modules_src |_MyPackage.pm | |_mycode.pl
And inside MyPackage.pm looks like this
package Sth:MyPackage; use 5.005; use strict; use vars qw( @ISA $VERSION ); # etc2... 1;
I am aware and have tested that executing mycode.pl is perfectly fine when I moved MyPackage.pm outside modules_src/, and use MyPackage; in mycode.pl. But my intention is to group those packages in one subdirectory.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Howto Call Package from a Subdirectory
by almut (Canon) on May 06, 2007 at 16:38 UTC

    You could do

    use lib 'modules_src'; use MyPackage;

    or

    BEGIN { require 'modules_src/MyPackage.pm'; # (call import() here, if required...) }

    (assuming your current working directory is set such that it contains modules_src; otherwise specify the absolute path)

    Though, personally, I prefer to stick to the convention, i.e. if your module is named Sth::MyPackage, then put it in a subdirectory named accordingly: modules_src/Sth/MyPackage.pm. Then, you can write

    use lib 'modules_src'; use Sth::MyPackage;

    and have your use statement properly indicate the module's namespace...

Re: Howto Call Package from a Subdirectory
by shmem (Chancellor) on May 06, 2007 at 17:08 UTC
    The overall code directory structure look simply like this:
    Since the package you want to load is identifiying itself as Sth::MyPackage, you must put the file MyPackage.pm into a directory named Sth and include the parent directory of Sth in @INC.

    From the docs (perldoc -f require):

    In other words, if you try this:
    require Foo::Bar; # a splendid bareword
    The require function will actually look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

    And use is "an enhanced version of require", so to speak. See perlfaq8 for differences between do, require and use.

    You can add the parent of Sth (say, ~/MyPerl/modules_src) with

    use lib '/home/neversaint/MyPerl/modules_src'; # no tilde expansion

    or including that path in your PERL5LIB environment variable.

    --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}
Re: Howto Call Package from a Subdirectory
by trwww (Priest) on May 06, 2007 at 20:02 UTC

    The other suggestions are correct, you want to:

    use lib '/path/to/modules_src';

    But this isn't much fun maintanence wise, because when you move around your application directory you have to update this line of code. The FindBin module, which has come with perl for a while now, takes care of this problem:

    use FindBin; use lib "$FindBin::Bin/../modules_src";

    This way you can move around the application directory at will and @INC will get set properly without modifying the code.

    EDIT:

    Based on your application directory's structure, you would want to use:

    use FindBin; use lib "$FindBin::Bin/modules_src";

    Because the program you are ruuning is in the root of your application directory

        That works, more or less, but gives me a warning, on Windows/perl5.6.1: Use of uninitialized value in concatenation (.) or string at [...]/lib/File/Spec/Win32.pm line 240.

        The reason is the lack of the third parameter to catpath.

        What's more, it still has a trailing backslash (probably a slash on Unix), which is not my idea of ideal. It might work, or it might fail on some platforms.

        The following code no longer produces the warning, but I still get the trailing backslash:

        $RealBin = catpath ((splitpath (rel2abs ($0)))[0,1], '');

        File::Spec is dumb.

Re: Howto Call Package from a Subdirectory
by shigetsu (Hermit) on May 06, 2007 at 16:21 UTC
    You probably want lib.
Re: Howto Call Package from a Subdirectory
by Anonymous Monk on May 07, 2007 at 07:25 UTC
Re: Howto Call Package from a Subdirectory
by Anonymous Monk on May 08, 2007 at 06:54 UTC
    Hi, I think if you use
    use lib 'modules_src';
    use MyPackage;

    ....

    should work fine.

    Thanks,
    -vijay
Re: Howto Call Package from a Subdirectory
by 13warrior (Acolyte) on May 08, 2007 at 13:02 UTC
    Hi, When modules are not installed in the default locations where they need to be, you could use the following #!/usr/bin/perl -w use lib 'location of your module'; ... _H.T.H 13Warrior
Re: Howto Call Package from a Subdirectory
by Anonymous Monk on May 08, 2007 at 06:53 UTC
    Hi, I think if you use
    use lib 'modules_src';
    use MyPackage;<br.
    ....

    should work fine.

    Thanks,
    -vijay