SG::Cfg - Perl extension to provide a config repository.
use SG::Cfg;
my %cfg; tie %cfg, 'SG::Cfg';
$cfg{hostname} = sub { print "Enter the hostname: "; my $hn = <STDIN>; chomp $hn; return $hn; };
print "The hostname is $cfg{hostname}\n";
Cfg is a reimplementation of the very flexibile config system used by Unattended (unattended.sourceforge.net). The idea is that it manages a tied hash of config variables. This appears as a normal hash to your program, but actually has some code behind the scenes.
You can either initialise the hash values with normal variables:
$cfg{proxy} = "proxy.domain.com";
or they can be initialised with a perl function (sub) that calculates the variable value.
$cfg{hostname} = sub { print "Enter the hostname: "; my $hn = <STDIN>; chomp $hn; return $hn; };
Whether a variable holds a sub or a value, you access it the same way.
print $cfg{hostname};
The sub is only called the first time the variable is accessed. When it returns, the return value replaces it in the hash. So it is returned directly the next time.
So however many times you access the variable, the sub is only ever called once.
The sub doesn't have to ask the user for the value, it can calculate or retrieve it however it wants.
use SG::Cfg;
tie %cfg, 'SG::Cfg';
$cfg{hostname} = sub { print "Enter the hostname: "; my $hn = <STDIN>; chomp $hn; return $hn; };
$cfg{domain} = sub { print "Enter the domain: "; my $hn = <STDIN>; chomp $hn; return $hn; };
$cfg{fqdn} = sub { return $cfg{hostname} . "." . $cfg{domain}; };
my $fqdn = $cfg{fqdn};
print "The fully qualified domain name is: $fqdn\n";
When this is run you will see:
Enter the hostname: www Enter the domain: microsoft.com The fully qualifified domain name is www.microsoft.com.
As the sub is only run once, this is no good for items which change over time. As Cfg operates as a cache, it will always return the same value.
None by default.
The home page for this module is at http://snakegully.sourceforge.net.
The package can be downloaded at http://sourceforge.net/projects/snakegully/.
If you need to automatically configure and deploy Windows systems, you should look at the 'Unattended' system at http://unattended.sourceforge.net/, which contains the functionality that this module is based on.
Darryl Luff, darryl@snakegully.nu.
Copyright (c) 2006 by Darryl Luff
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.