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

Conditional 'use lib'

by bjdean (Novice)
on Feb 26, 2003 at 07:48 UTC ( [id://238691]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings folks,

I've recently come across a problem whereby I'd like to conditionally use 'use lib' (which doesn't work due to the early order of processing 'use' in compilation).

The question then, is there a better way then this:

BEGIN {
    use Sys::Hostname;
    if ( hostname() eq 'foo.bar.org' ) {
        unshift @INC, ('/alternate/libdir');
    } else {
        unshift @INC, ('/default/libdir');
    }
}

To achieve something close to to 'meaning' of:

BEGIN {
    use Sys::Hostname;
    if ( hostname() eq 'foo1.bar.org' ) {
        use lib '/alternate/libdir';
    } else {
        use lib '/default/libdir';
    }
}

Replies are listed 'Best First'.
Re: Conditional 'use lib'
by Chmrr (Vicar) on Feb 26, 2003 at 08:04 UTC

    The work is done inside of lib.pm's import method, so you might try:

    BEGIN { use Sys::Hostname; require lib; if (hostname() eq 'foo.bar.org') { lib->import('/alternate/libdir'); } else { lib->import('/default/libdir'); } }

    The advantage of this over the unshift trick is that you'll get architecture-dependent paths added, as well.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Just found an even easier way, which seems to work for me: my $module = "ROX::Filer"; eval "use $module"; die "couldn't load $module : $!n" if ($@); See http://weierophinney.net/matthew/archives/23-conditional-use-in-perl.html
Re: Conditional 'use lib'
by Abigail-II (Bishop) on Feb 26, 2003 at 08:00 UTC
    This should work, but I've no experience myself with the if module.
    use if hostname eq 'foo1.bar.org', lib => '/alternate/libdir'; use if hostname ne 'foo1.bar.org', lib => '/default/libdir';

    Abigail

Re: Conditional 'use lib'
by PodMaster (Abbot) on Feb 26, 2003 at 08:03 UTC
    Do you know what use does?
    use lib ( 1 ? 'foo' : 'bar' ); die @INC;
    Please read `perldoc -f use', `perldoc -f import'


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Conditional 'use lib'
by dbp (Pilgrim) on Feb 26, 2003 at 08:11 UTC
    If you look at the perldoc for lib you'll see that all it does besides unshift the path into @INC is add architechture specific directories if it finds them and remove trailing duplicates from @INC. A quick look at the lib module's source should give you an idea of how to proceed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-12-05 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (36 votes). Check out past polls.