Dan Muey wrote:
> > Hamish Whittal wrote:
> > > Hi Everyone,
> > >
> > > I am needing to use some variables throughout all the modules
> > > that I have created. I was wondering, bar setting them at the top
> > > of each module, what the best means of doing this is. Perhaps
> > > setting an environment variable, but what other ideas. The
> > > ultimate goal is to be able to change the value in the
> > > 'top-level' module and that should have an impact throughout all
> > > subsequent modules.
> >
> > Create a module like this:
> >
> > Common.pm
> > ---------
> > package Common;
> >
> > use strict;
> > use base 'Exporter';
> >
> > our @EXPORT = qw/$foo @bar %baz/;
> >
> > 1;
> >
> > Now, in each of your modules, add "use Common;"
> >
> > The variables $foo, $bar, $baz will now be shared globals
> > across all the modules that "use Common". Changes to the
> > value of one of these in any module will be visible across
> > all the modules, since they are all aliasing the same set of
> > variables.
>
> Will this also work if I was to use Common; in a script?
Sure. "use Common" translates to (essentially):
require Common;
Common->import();
The call to import creates an alias in the importing package for each of the
items in Common's @EXPORT list.
>
> IE
>
> #!/perl -w
>
> use strict;
> use Common; # IE example above
> print $foo;
> for(@bar) {
> if(exists $baz{$_}) { }
> }
Yes.
>
> Also in the example above where do I cactually put values in those
> variables?
Doesn't matter. You can assign to them in any package that has "use
Common;", or in Common.pm itself. The latter is typical, but by no means
required.
>
> package Common;
>
> use strict;
> use base 'Exporter';
>
> $foo = "HI";
> our @EXPORT = qw/$foo @bar %baz/;
> # or here : $foo = "HI";
Either place. But you need "our $foo", to make "use strict" happy.
n.b. you *don't* need the "our" declaration in any module that includes "use
Common". Importing a symbol is good enough to make "use strict" happy.
>
> 1;
>
> >
> > (If you already have a common module used by all the other
> > modules, just add the "use base" and "our @EXPORT" lines to that
> > module.)
> >
> > perldoc Exporter (be sure to read this to learn about other
> > options, and why the practice of using @EXPORT is generally
> > discouraged).
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]