SPUG: conditional module loading

David Dyck david.dyck at fluke.com
Fri Jul 13 22:38:59 PDT 2007


On Fri, 13 Jul 2007 at 22:12 -0700, Charles Connolly wrote:

>     I would like to load one of two  modules depending on the value of an
> environment variable. It looks like both modules are loaded in spite of the
> 'use' statements being in an if/else block, and the 'elsif' part not being
> entered. Any ideas what's going on? I'm stumped. Thanks for your thoughts.
>
> Here's the test script test.plx:
> #! /usr/bin/perl -w
> use strict;
>
> BEGIN {
>    if ( $ENV{ test_status } == 1 ) {
> 	   use TestMod;
> 	   warn "using TestMod\n";
>    } elsif ( $ENV{ test_status } == 2 ) {
> 	   use Test2Mod;
> 	   warn "using Test2Mod\n";
>    } else {
> 	   die "unknown value of test_status: $ENV{ test_status }\n";
>    }
> }

The use statement is evaluated at compile time 
(so you are on the right track to start with a BEGIN block.)

try changing your use statements to
 	eval "use TestMod";
and
 	eval "use Test2Mod";


More information about the spug-list mailing list