[Canberra-pm] do read-local.config
Paul Fenwick
pjf at perltraining.com.au
Thu Mar 30 03:28:45 PST 2006
G'day Michael,
Michael James wrote:
> Hang ON! Lets get this clear, the done.config file
> can't create a variable visible to the calling program,
> nor see a pre-existing variable to change it.
That's absolutely correct. Lexical scope is very strict, and never crosses file
boundaries.
> What's the point?
The file that is being "done" can *return* a value to the calling code:
my $config = do "configuration.pl";
If the configuration file happens to return a hash-reference, then we can return
a whole bunch of useful configuration at once. For example, 'configuration.pl'
may contain:
return {
directory => "/usr/local/example",
timeout => 30,
hostname => "www.example.com",
}
This is much less error-prone than creating or modifying variables and expecting
the other file to know what they're called. By using:
my $config = do "configuration.pl";
we may it very clear to anyone reading the code that we expect to store the
result of our file in the $config variable.
More notes can be found at http://perl.net.au/wiki/Using_do_for_configuration .
Please feel free to improve or adjust as you see fit.
All the very best,
Paul
--
Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training | Ph: +61 3 9354 6001
Perl Training Australia | Fax: +61 3 9354 2681
More information about the Canberra-pm
mailing list