in reply to Re: Packages and modules
in thread Packages and modules
It's hard to know for certain what the right approach is without knowing more about what you're doing. However, based on the information you've given, it sounds like an import method would probably be overkill. If you have just a single value to pass in, the approach I'd take is probably to provide a package method to set the variable; this can return its value if you want the outside world to see it. Here's a trivial example module:
And here's a test script:package Foobar; my $var; # If called with no arguments, just returns the value. Otherwise sets +and returns. sub foovar { $var = shift if @_; return $var; } # This version won't allow the value to be set twice sub foovar_once { die "Attempt to foovar more than once\n" if @_ && defined($var); $var = shift if @_; return $var; } 1;
use Foobar; if (1) { Foobar::foovar(7); print "1. The value is now ", Foobar::foovar(), "\n"; Foobar::foovar(9); print "2. The value is now ", Foobar::foovar(), "\n"; } else { Foobar::foovar_once(7); print "1. The value is now ", Foobar::foovar_once(), "\n"; Foobar::foovar_once(9); print "2. The value is now ", Foobar::foovar_once(), "\n"; }
Change the 1 to a 0 in the if test to see the other behavior.
HTH,
--roundboy
|
|---|