in reply to Using packages to store global configuration vars
1. As already mentioned, my vars are only visible within the same scope, pretty much no matter what you do. Use our instead if you want them to be visible in other packages or files.
2. Just changing my to our will allow you to reference the variables from your main program as $Config::database. If you want to be able to use just $database in the main program, then the Config module will need to export them, using Exporter:
The main program, then, would start off withpackage Config; use strict; use warnings; use base 'Exporter'; BEGIN { # Things to export by default - minimize this, if you use it at all our @EXPORT = qw( ); # Things to export when the calling code requests them our @EXPORT_OK = qw( \$database \$tmplfile \$blog_title ); # A handy way of grouping exported items our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] ); } our $database = 'data/site.db'; our $tmplfile = 'templates/index.html'; our $blog_title = 'example.com'; 1;
#!/path/to/perl use strict; use warnings; use lib qw(/var/www/blogapp); use Config qw($database, $tmplfile, $blog_name); # or just # use Config qw(:all); # if you want everything the Config module exports
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using packages to store global configuration vars
by fuzzyping (Chaplain) on Sep 06, 2009 at 13:34 UTC | |
by dsheroh (Monsignor) on Sep 06, 2009 at 15:42 UTC | |
by fuzzyping (Chaplain) on Sep 06, 2009 at 18:09 UTC | |
by LanX (Saint) on Sep 06, 2009 at 18:43 UTC | |
by fuzzyping (Chaplain) on Sep 06, 2009 at 22:15 UTC | |
by LanX (Saint) on Sep 06, 2009 at 23:26 UTC |