Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Referring to a module above the CWD

by Spidy (Chaplain)
on Jun 16, 2007 at 20:20 UTC ( #621617=perlquestion: print w/ replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, fellow monks.

I am currently working on a web application that involves at least 50% of the component scripts accessing a database. This means that a lot of scripts need to have the information required to connect to said database, and in that spirit I have created a module(config.pm) which they use.

However, I am having a problem.

The configuration module is within the root directory for the project. I have a few scripts that are inside directories below the root directory, and would like to keep using the one configuration module. Does anyone know how I could use a module located at ../config.pm in relation to the script that needs to access it?

Thanks,
Spidy

Comment on Referring to a module above the CWD
Select or Download Code
Re: Referring to a module above the CWD
by FunkyMonk (Canon) on Jun 16, 2007 at 20:29 UTC
Re: Referring to a module above the CWD
by merlyn (Sage) on Jun 16, 2007 at 21:33 UTC
    Your question is slightly confusing, and as such, the answers so far are also broken.

    Do you want:

    1. A module in the directory above where the script is located? or
    2. A module in the directory above the current working directory?
    These are two very different questions, and require different answers. Hint: adding ".." to the @INC path (whatever way you do that) accomplishes #2 but not #1. And you mention CWD, which also hints at #2. But I really think you might want the answer to #1 instead, as that's usually the one that most people really end up needing.

    As a way of understanding the difference, suppose I issued the following:

    cd /usr/tmp /long/path/to/your/script with some args
    Would you want to look in "/long/path/to/your" (#1) or "/usr" (#2)?
      Hi merlyn,

      For the moment, I'm only looking for the answer to the second question.

      Later in this thread, Spidy, you say you want #2, but if someone else reading this wants the answer to #1, the "FindBin" module comes in handy.

      E.g. suppose you have a top directory, and underneath it you have bin and lib, and under lib you have your modules. You could use:
      use FindBin qw($RealBin); use lib ("$RealBin/../lib");
      To access your modules.

      In this case, $RealBin will be set to the directory of your script, and if you have a set way to navigate to your libraries from that directory, you can make use of that.

      This technique can be useful, for example, if you have multiple versions of an application + set of libraries around, and want them both to be installed at the same time without version conflicts.


      --JAS

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-08-27 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (252 votes), past polls