Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 + Bishop.pm + King.pm + Knight.pm + Pawn.pm + Board.pm + Piece.pm + Square.pm + Tests + Bishop.pl + King.pl + Knight.pl + Pawn.pl + Square.pl

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!

Regards,

Tommy

Comment on How to use a .pm file in a .pl file in another directory.
Select or Download Code
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 Marshall (Prior) 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 Piece.pm 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/Piece.pm" — 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/Piece.pm"
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!

    Regards,

    Tommy

Re: How to use a .pm file in a .pl file in another directory.
by toolic (Chancellor) 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.


        Dave

Re: How to use a .pm file in a .pl file in another directory.
by Perlbotics (Abbot) 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 + Bishop.pm + King.pm + Knight.pm + Pawn.pm + Board.pm + Piece.pm + Square.pm +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.

      Regards,

      Tommy

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
    Hi,

    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/Blah.pm 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950591]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-07-30 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls