SPUG: conditional module loading

DeRykus, Charles E charles.e.derykus at boeing.com
Sat Jul 14 10:28:30 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";
>    }
> }

As another alternative,  you could just load at runtime 
before the call to print_id() occurs:

   #!/usr/bin/perl
   ...
   if ( $ENV{ test_status } == 1 ) {
       require TestMod; 
       import TestMod qw/print_id/;
   elsif ( $ENV{ test_status } == 2 ) {
       require Test2Mod; 
       import Test2Mod qw/print_id/;
   ...
   }
   ...
   print "status is $ENV{ test_status }\n"; print_id();


--
Charles DeRykus



More information about the spug-list mailing list