Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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:

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';
    }
}

Comment on Conditional 'use lib'
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 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 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
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? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-07-13 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls