Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Conditional 'use lib'

by bjdean (Initiate)
on Feb 26, 2003 at 07:48 UTC ( #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:

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

To achieve something close to to 'meaning' of:

    use Sys::Hostname;
    if ( hostname() eq '' ) {
        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's import method, so you might try:

    BEGIN { use Sys::Hostname; require lib; if (hostname() eq '') { 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
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 '', lib => '/alternate/libdir'; use if hostname ne '', lib => '/default/libdir';


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://238691]
Approved by JayBonci
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-07-20 22:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (316 votes). Check out past polls.