Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Variable Declaration

by andreychek (Parson)
on Oct 19, 2001 at 17:30 UTC ( #119978=note: print w/ replies, xml ) Need Help??


in reply to Variable Declaration

Of the many ways you could go about doing this, one possible method is to write a configuration file for your program.

The mod_perl guide has a section on writing configuration files. While it is part of the mod_perl guide, by no means do you need to be using mod_perl to make use of it.

They take you from the quick, "false lazy" method of writing them, to a very nice, flexible system. As a spoiler, this is the style config file they end up leading you to. The code I'm pasting here is directly out of the mod_perl guide, I did not write it myself, although I did add a few comments:

# Config Module package My::Config; use strict; use vars qw(%c); # Create a hash containing our config data %c = ( dir => { cgi => "/home/httpd/perl", docs => "/home/httpd/docs", img => "/home/httpd/docs/images", }, url => { cgi => "/perl", docs => "/", img => "/images", }, color => { hint => "#777777", warn => "#990066", normal => "#000000", }, ); # Main program code (in a seperate file of course) use strict; use My::Config (); use vars qw(%c); *c = \%My::Config::c; print "Content-type: text/plain\r\n\r\n"; print "My url docs root: $c{url}{docs}\n";
Again, this code is directly from the mod_perl guide. So what they are offering here is basically a big hash, which is made global. And whatever modules you are creating are capable of making use of that existing hash, by referencing the full package variable name. They just make an alias to that package variable by doing "*c = \%My::Config::c". And anywhere in your program, you can access the data in that config file by just saying "$c{url}{docs}" and the like.

Have fun!
-Eric


Comment on Re: Variable Declaration
Download Code
Re: Re: Variable Declaration
by buckaduck (Chaplain) on Oct 19, 2001 at 18:25 UTC
    Wouldn't it be just as simple to have the configuration module return a hash reference via a function? That way, the program is somewhat easier to read:
    use strict; use My::Config 'GetConfig'; my $config = GetConfig(); print "Content-type: text/plain\r\n\r\n"; print "My url docs root: $config->{url}{docs}\n";

    buckaduck

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://119978]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-10-25 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (147 votes), past polls