Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

using my module.pm in different directory error

by t-rex (Scribe)
on Aug 10, 2016 at 06:22 UTC ( [id://1169470]=perlquestion: print w/replies, xml ) Need Help??

t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, This is my directory structure

urs.pl src now this src contains server and client directories src-> server client and client contains Yaml_Lib.pm Client_lib.pm Run_lib.pm

now the urs.pl uses the files Yaml_libm.pm and others in the client folder.

I want to know how should I include them in my urs.pl , I checked on this site and I got to know about use lib ‘..’;

but when I do it I still get error that Yaml_Lib.pm and others not found in @INC

use lib ‘src/client/’; use Yaml_Lib.pm use Client_lib.pm

Note that this structure (entire scripts) can be in any directory eg; now my pwd is different it could be different on other machine but inside structure(scr/client/) remians constant.

Replies are listed 'Best First'.
Re: using my module.pm in different directory error
by haukex (Archbishop) on Aug 10, 2016 at 09:41 UTC

    Hi t-rex,

    If your module files are always located in the same place in the file system, use

    use lib '/absolute/path/to/modules';

    If your module files are always located in the same place relative to the script, use

    use FindBin; use lib "$FindBin::Bin/path/to/modules";

    Both solutions are independent of the current working directory because they use absolute paths.

    Also, you wrote "use Yaml_Lib.pm", I'm assuming you meant "use Yaml_Lib;" because the .pm extension is added automatically by Perl.

    Hope this helps,
    -- Hauke D

Re: using my module.pm in different directory error
by beech (Parson) on Aug 10, 2016 at 06:44 UTC
Re: using my module.pm in different directory error
by hippo (Bishop) on Aug 10, 2016 at 09:01 UTC

    Quick and dirty: PERL5LIB

    Long and clean: use proper namespaces and package your script and modules into a distribution as you would for CPAN. eg. put your modules under a namespace like "App::URS::" which would equate to a directory like App/URS/, install them into the site_perl tree and then your script should use them with lines like:

    use App::URS::YAML;

    You'll probably have to read up on packages and it would benefit to look at Module::Starter or similar. See also Simple Module Tutorial and How to make a CPAN Module Distribution. It's a long road for sure but well worth it and will make problems like the one you currently have just vanish.

Re: using my module.pm in different directory error
by duyet (Friar) on Aug 10, 2016 at 08:47 UTC
    Add a BEGIN{} to your urs.pl which add paths of your pm files to @INC:
    BEGIN { unshift @INC, './path/to/lib/dir1'; unshift @INC, '/full/path/to/lib/dir2'; }
      path to lib which change as in lets say i launch script from /tmp/ something and next i launch it from /home/userx/run_auto.... so the path inside run_auto remains constant but where i launch the script from keeps changing...
        I am not sure to understand. Are you saying that your modules will not always be in the same place? Or the opposite?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found