Perl Programming Page 5 - Introduction to mod_perl (part 4): Perl Basics |
As the title says you can import a variable into a script or modulewithout using package My::Config; use strict; use vars qw(%c); %c = ( # All the configs go here scalar_var => 5, array_var => [qw(foo bar)],
hash_var => {
foo => 'Foo',
bar => 'BARRR',
},
);
1;
Now in packages that want to use the configuration variables I haveeither to use the fully qualified names like package My::HTML; use strict; use lib qw(.); # Global Configuration now aliased to global %c use My::Config (); # My/Config.pm in the same dir as script.pl use vars qw(%c); *c = \%My::Config::c;
# Now you can access the variables from the My::Config
print $c{scalar_var};
print $c{array_var}[0];
print $c{hash_var}{foo};
Of course $c is global everywhere you use it as described above, andif you change it somewhere it will affect any other packages you havealiased Note that aliases work either with global or my *c = \%My::Config::c; # ERROR! Which is an error. But you can write: local *c = \%My::Config::c; For more information about aliasing, refer to the Camel book, secondedition, pages 51-52.
blog comments powered by Disqus |