Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How to use a .pm file in a .pl file in another directory.

by tommyxd (Acolyte)
on Jan 29, 2012 at 16:21 UTC ( #950591=perlquestion: print w/replies, xml ) Need Help??
tommyxd has asked for the wisdom of the Perl Monks concerning the following question:

Hello, everyone!

I'm currently working on a Perl chess project and I have all my modules in separate .pm files. My directory tree is looking like this:

+ Chess + Piece + + + + + + + + Tests + + + + +

I'm currently trying to write some tests, and I can't seem to be able to access any of the .pm files in the Chess/ or Chess/Piece directories. I kept getting the error: "Can't locate *.pm in @INC (@INC contains: ...)", so I tried adding my working directory to @INC with a code like this:

BEGIN { push @INC, "path/to/directory/of/chess-square"; } use Chess::Square;

This got me nowhere, so I tried this as an alternative:

use lib "path/to/directory/of/chess-square"; use Chess::Square;

This didn't work either..

So, PerlMonks, can you help me out? How can I write the following code with my directory structure being as it is?

use Chess::Square; use strict; my $square = Chess::Square->new();

I'm using Windows and ActiveState Perl.

Any help will be greatly appreciated!



Replies are listed 'Best First'.
Re: How to use a .pm file in a .pl file in another directory.
by Perlbotics (Chancellor) on Jan 29, 2012 at 22:04 UTC

    Your original question seems to be answered, but maybe this would be an opportunity to re-factor your project into something that is a little closer to common Perl project structure? Doing so would allow you to profit from existing tools that support building, testing, packaging, installing, etc. modules.

    First step would be to move your modules into lib, move your tests into t, and rename tests *.pl to *.t. If you're already using Test::*, running prove -l should be sufficient to run the test suite. Another approach would be to let something like Module::Starter create an initial directory structure including all the templates and standard files. The latter approach might be a little bit more work concerning importing an already existing project, but I assume it will pay off in the long run.

    +lib + Chess + Piece + + + + + + + +t + Bishop.t + King.t + Knight.t + Pawn.t + Square.t

    See also Test::Simple, Test::More, Module::Starter(::PBP) (chose Module::Build or ExtUtils::MakeMaker), maybe Dist::Zilla. Meditation: RFC: How to Release Modules on CPAN in 2011

      Thank you for your suggestion!

      To be honest, I haven't given much thought on the subject of project structure. I'm currently testing with Test::More and running the test suite with a single command looks pretty great! I'll definitely reorganize my directory structure to a one that is more common for Perl.



Re: How to use a .pm file in a .pl file in another directory.
by Eliya (Vicar) on Jan 29, 2012 at 16:33 UTC

    It's not quite clear from your pseudo path, whether you specified a relative path.  If so, make it an absolute one (e.g. "c:/path/to/directory/of/chess-square"). In this case, both use lib ... and BEGIN { push @INC, ... } should work just fine (if you specify the correct path).

Re: How to use a .pm file in a .pl file in another directory.
by toolic (Bishop) on Jan 29, 2012 at 18:29 UTC

      I've already done that, but the thing is that this is a project for a university course(probably forgot to mention this) and there's not much left for me to do if I use a module directly from CPAN, now is there? ;)

      The purpose is to learn stuff after all. And finding out the answer to my question counts.

      Anyway, thank you for your effort!

        "The purpose is to learn stuff after all."

        I wish I could upvote that more than once.


Re: How to use a .pm file in a .pl file in another directory.
by Marshall (Abbot) on Jan 29, 2012 at 16:35 UTC
    I think you need something like this: use lib "path/to/Chess path/to/Chess/Piece";
    use lib "path/to/Chess"; use Chess::Piece::Knight; use Chess::Piece;
    Oh, geeze this Piece directory as well as looks like trouble to me.
      use lib "path/to/Chess path/to/Chess/Piece";

      In this case, when you try to use Chess::Piece, Perl would look for "path/to/Chess path/to/Chess/Piece/Chess/" — so that likely won't work.

      Perl essentially does the following to create the full path:

      use lib "searchpath"; use Chess::Piece; / / .pm => "searchpath/Chess/"
Re: How to use a .pm file in a .pl file in another directory.
by tommyxd (Acolyte) on Jan 29, 2012 at 17:30 UTC

    Thank you so much, guys, for your contribution!

    The problem was that I was passing the absolute path to the directory, containing the file I wanted to use..

    Silly me.

    Anyway, my problem is solved.

    Thanks once again, I really appreciate your help!



Re: How to use a .pm file in a .pl file in another directory.
by kielstirling (Scribe) on Jan 30, 2012 at 05:31 UTC

    For a local library directory I use the following. It relies on a lib directory existing within your project. i.e. /your/project/lib

    use FindBin qw($Bin); use lib "$Bin/lib";

    You can then create your modules within this directory. i.e. /your/project/lib/Foo/ Then in your code use it like

    use FindBin qw($Bin); use lib "$Bin/lib"; use Foo::Blah

    Have fun !!

    -Kiel R Stirling

      I'll give that a shot as well, thanks, Kiel! :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950591]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-09-20 17:47 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (237 votes). Check out past polls.