H
Hal Vaughan
I'm having a problem understanding namespace with modules. If I have a
program and it uses some modules (say mod1, mod2, and mod3), I thought that
the names I export from those modules are available in the main program's
namespace -- and ONLY in that program's namespace.
I also was under the understanding that any variables global to, say, mod2,
when set from the main program, would ONLY be set in that "instance" of
mod2. So if, for example, mod1 also used mod2 (with a separate "use mod2;"
statement within mod1), any global variables set in the main program's
instance of mod2 should be in a completely different namespace from the one
within mod1, where mod1 uses mod2. In other words, while both the main
program and mod1 use mod2, the program's mod2 should maintain variables
separately from mod1's mod2.
However when I run tests, that is not the case (code and sample run included
below). When I test it, if I make changes in the main program's mod2, they
show up in mod1's mod2, and vice versa. For me, this is actually a
benefit, but it is not how I understood namespace to work.
Is this the behavior I should expect? Is it documented or intended, so I
can count on it always happening? If I use a module (say mod1) in the main
program, will, and use a number of other modules from the main program, if
any of those other modules use mod1, will the variables in each instance of
mod1 stay consistant with each other?
Thanks for any comments. This is rather difficult for me to follow and make
sure I understand it correctly.
Hal
----------------------------------------------------------------------
Program code:
#!/usr/bin/perl
use fakemods::mod1;
use fakemods::mod2;
use fakemods::mod3;
print "This is the main program\n";
setval1(1); printval1();
setval12(2); printval2();
setvalx12(3); printval2(); printvalx12();
setval2(4); printval2(); printvalx12();
----------------
fakemods/Mod1.pm listing:
package fakemods::mod1;
use Exporter;
use fakemods::mod2;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval1 printval1 setval11 setval12 setval13
setvalx12 printvalx12);
}
sub setval1 {
$mainval = $_[0];
return;
}
sub printval1 {
print "Value 1: $mainval\n";
return;
}
sub setval11 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval12 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval13 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
sub setvalx12 {
my $val = shift(@_);
setval2($val);
return;
}
sub printvalx12 {
printval2();
return;
}
1;
----------------
fakemods/Mod2.pm listing:
package fakemods::mod2;
use Exporter;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval2 printval2 setval21 setval22 setval23);
}
sub setval2 {
$mainval = $_[0];
return;
}
sub printval2 {
print "Value 2: $mainval\n";
return;
}
sub setval21 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval22 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval23 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
1;
----------------
fakemods/Mod3.pm listing:
package fakemods::mod3;
use Exporter;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval3 printval3 setval31 setval32 setval33);
}
sub setval3 {
$mainval = $_[0];
return;
}
sub printval3 {
print "Value 3: $mainval\n";
return;
}
sub setval31 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval32 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval33 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
1;
Test output:
----------------
[hal@workstation:~]$ modtest
This is the main program
Value 1: 1
Value 2: 2
Value 2: 3
Value 2: 3
Value 2: 4
Value 2: 4
program and it uses some modules (say mod1, mod2, and mod3), I thought that
the names I export from those modules are available in the main program's
namespace -- and ONLY in that program's namespace.
I also was under the understanding that any variables global to, say, mod2,
when set from the main program, would ONLY be set in that "instance" of
mod2. So if, for example, mod1 also used mod2 (with a separate "use mod2;"
statement within mod1), any global variables set in the main program's
instance of mod2 should be in a completely different namespace from the one
within mod1, where mod1 uses mod2. In other words, while both the main
program and mod1 use mod2, the program's mod2 should maintain variables
separately from mod1's mod2.
However when I run tests, that is not the case (code and sample run included
below). When I test it, if I make changes in the main program's mod2, they
show up in mod1's mod2, and vice versa. For me, this is actually a
benefit, but it is not how I understood namespace to work.
Is this the behavior I should expect? Is it documented or intended, so I
can count on it always happening? If I use a module (say mod1) in the main
program, will, and use a number of other modules from the main program, if
any of those other modules use mod1, will the variables in each instance of
mod1 stay consistant with each other?
Thanks for any comments. This is rather difficult for me to follow and make
sure I understand it correctly.
Hal
----------------------------------------------------------------------
Program code:
#!/usr/bin/perl
use fakemods::mod1;
use fakemods::mod2;
use fakemods::mod3;
print "This is the main program\n";
setval1(1); printval1();
setval12(2); printval2();
setvalx12(3); printval2(); printvalx12();
setval2(4); printval2(); printvalx12();
----------------
fakemods/Mod1.pm listing:
package fakemods::mod1;
use Exporter;
use fakemods::mod2;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval1 printval1 setval11 setval12 setval13
setvalx12 printvalx12);
}
sub setval1 {
$mainval = $_[0];
return;
}
sub printval1 {
print "Value 1: $mainval\n";
return;
}
sub setval11 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval12 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval13 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
sub setvalx12 {
my $val = shift(@_);
setval2($val);
return;
}
sub printvalx12 {
printval2();
return;
}
1;
----------------
fakemods/Mod2.pm listing:
package fakemods::mod2;
use Exporter;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval2 printval2 setval21 setval22 setval23);
}
sub setval2 {
$mainval = $_[0];
return;
}
sub printval2 {
print "Value 2: $mainval\n";
return;
}
sub setval21 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval22 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval23 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
1;
----------------
fakemods/Mod3.pm listing:
package fakemods::mod3;
use Exporter;
use strict;
our ($mainval);
BEGIN {
use vars qw(@ISA);
@ISA = qw(Exporter);
our @EXPORT = qw(setval3 printval3 setval31 setval32 setval33);
}
sub setval3 {
$mainval = $_[0];
return;
}
sub printval3 {
print "Value 3: $mainval\n";
return;
}
sub setval31 {
my $val = shift(@_);
main::fakemods::mod1::setval1($val);
return;
}
sub setval32 {
my $val = shift(@_);
main::fakemods::mod2::setval2($val);
return;
}
sub setval33 {
my $val = shift(@_);
main::fakemods::mod3::setval3($val);
return;
}
1;
Test output:
----------------
[hal@workstation:~]$ modtest
This is the main program
Value 1: 1
Value 2: 2
Value 2: 3
Value 2: 3
Value 2: 4
Value 2: 4