Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How can you make 'use lib "foo"; dynamic?

by martell (Hermit)
on Jul 31, 2005 at 16:08 UTC ( [id://479709]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

While programming over the past, i have created some personal generic perl classes that i use regularly in different small programs. I consider these classes not generic enough to become real CPAN modules. Mostly they add some syntax sugar to CPAN modules to keep the programming style consistent or hide some complexity.

But now i end up with adding at different perl classes some line like:

use lib "personal_library\personal_class";

This is not very portable nor handy. It imposes the location of my library. I want to be able to set the location of my library more dynamic.

Now, most of my programs have some xml file with configuration information. It is easy to add a variable in the configuration file and then obtain it from the file.

Rest the following question: Is it possible to add the path of my personal library to the @INC array globally at execution time for my program so i can omit the 'use lib' line in my modules? Or is there a more perlish way to handle this problem?

Thanks in advance

Replies are listed 'Best First'.
Re: How can you make 'use lib "foo"; dynamic?
by perrin (Chancellor) on Jul 31, 2005 at 16:28 UTC
    You can use the FindBin module (in the standard distribution) to find your script's location and make the library path relative to that.
Re: How can you make 'use lib "foo"; dynamic?
by Tanktalus (Canon) on Jul 31, 2005 at 16:47 UTC

    Last time I suggested FindBin in Re: accessing perl libraries, tye complained that FindBin is broken. However, ignoring that for a second, the above post would be my suggestion, and is, in fact, how I solve the same problem. Because my scripts and modules are all stored in a version control system, I want to ensure that the same extraction of the script and the same extraction of the libraries are working together - and I can have different levels running at the same time. So each one sets up the lib to its own relative path, and everything works perfectly.

Re: How can you make 'use lib "foo"; dynamic?
by CountZero (Bishop) on Jul 31, 2005 at 16:15 UTC
    I just add my personal modules to the general perl/site/lib tree on my computer and have as toplevel of my modules something like My or MyModules so they don't interfere with similarly named CPAN-modules.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How can you make 'use lib "foo"; dynamic?
by Zaxo (Archbishop) on Jul 31, 2005 at 19:03 UTC

    If you set the PERL5LIB environment variable to the location of personal modules, it will be added to the head of @INC. For web services, environment variables can be set in the server configuration files. The gotcha is that taint mode will ignore PERL5LIB, just like any other environment variable.

    You'll need to make sure your configuration is read at compile time so that the location variable is already set in time to use.

    use vars qw/$config/; BEGIN { $config = do '/path/to/config.file'; } use lib $config->{'lib'};
    I've posited the simplest kind of perl configuration file here; modify to taste.

    After Compline,
    Zaxo

Re: How can you make 'use lib "foo"; dynamic?
by tlm (Prior) on Jul 31, 2005 at 19:18 UTC

    Maybe I'm misunderstanding what you're asking, but as long as you include the use lib in program that uses the modules in question, you don't need to include it in any of these modules/classes. (Example below.)

    the lowliest monk

Re: How can you make 'use lib "foo"; dynamic?
by jeteve (Pilgrim) on Aug 01, 2005 at 12:18 UTC
    Hi. You can
    push @INC , $yourPath ;
    At run time and then dynamically load your class (or module) with
    eval "require $yourModule;" ; if( $@ ){ die "Cannot load $yourModule : $@" ; }
    So at runtime, the use can specify where to find modules and which one to load. Hope it helps. Regards. J.
    Nice photos of naked perl sources here !
Re: How can you make 'use lib "foo"; dynamic?
by Anonymous Monk on Aug 01, 2005 at 13:51 UTC
    use lib; BEGIN { # Code to figure out which libraries you want to load. lib->import("your_library"); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found