SPUG: conditional module loading

Charles Connolly cconnoll at u.washington.edu
Fri Jul 13 22:12:43 PDT 2007


Hi,
     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.

Chuck Connolly

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

print "status is $ENV{ test_status }\n";
print_id();


# Here's me setting an environment variable and the output of the script:
> setenv test_status 1
> test.plx
Subroutine main::print_id redefined at /usr/lib/perl5/5.8.5/Exporter.pm line 65.
 at /home/chuck/bin/perllib/test.plx line 9
using TestMod
status is 1
I'm Test2Mod

# It looks like both modules are loaded, even though only the print statement in 
# the if block is called.

# Here's TestMod.pm:
#!/usr/bin/perl -w
use strict;

package TestMod;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw( print_id );

sub print_id {
    print "I'm TestMod\n";
}


1;


# Here's Test2Mod.pm:
#!/usr/bin/perl -w
use strict;

package Test2Mod;


require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw( print_id );

sub  print_id {

    print "I'm Test2Mod\n";
}

1;



More information about the spug-list mailing list