Pdoc
Config
Summary
Pdoc::Config - Configuration manager
Package variables
No package variables defined.
Included modules
Carp qw ( cluck confess )
FileHandle
Inherit
Synopsis
use Pdoc::Config;
# Creates config manager
my $config = Pdoc::Config->new();
# Assign a config param
$config->set('key', 'value');
# Load a config
$config->load('config.file');
# Store config
$config->save('newConfig.file');
Description
Pdoc::Config is an extremely simple module allowing to load/save configurations. Configurations are like hash tables, i.e. a key and a value with no restriction on the value.
Methods
Methods description
Pdoc::Config::_init is a private method used to define internal parameters. |
print 'Config val for "background" = ', $config->getVal('background'), "\n";
print 'Config val for "comment"->"start" = ', $config->getVal('comment','start'), "\n";
my $config = $config->getVal('comment');
print 'Config val for "comment"->"start" = ', $config->getVal($config,'start'), "\n";
Pdoc::Config::getVal gets a variable number of argument. Every argument defined as string will be used to access the config hash table. Every argument defined as hash reference will be used as is to access config data further down the represented hash. Multiple strings passed as argument will be used as multiple sub-nodes in the hash table. Returns the hash reference of the reached node, the value if reached the bottom of the hash tree or undef if nothing found. |
if (! $config->load('config.file')) {
cluck("Failed to load config file 'config.file'"); }
Pdoc::Config::load gets a file name as argument. It parses the file and keeps configuration parameters. The file format contains data as key=value in each line. Comments must start with the '#' character and must be in separate lines. Example:
# Configuration example
fgColor=blue
bgColor=white
Returns 1 on success and 0 on failure. |
my $config = Pdoc::Config->new();
Pdoc::Config::new creates a new configuration object. |
if (! $config->save('newConfig.file')) {
cluck("Failed to save config in file 'newConfig.file'"); }
Pdoc::Config::save gets a file name as argument. It stores its configuration in the file given as argument. Returns 1 on success and 0 on failure. |
$config->setVal('Html', 'Perl', 'Highlight', 'Var', 'Start', '');
Pdoc::Config::setVal gets a number of arguments representing the config nodes to create or descent with the last argument considered as the value to attribute. If only one argument is passed, the config parameter will automatically get a value of 1. |
Methods code
sub _init
{ my $self = shift;
$self->{'_loaded'} = 0;
return 1; } |
sub getVal
{ my $self = shift;
my @args = @_;
my $tree = $self->{'_config'};
foreach my $key (@args) {
if (ref $key eq 'HASH') {
$tree = $key;
next;
} else {
$key = uc $key;
}
if (! defined $tree->{$key}) {
return undef;
} else {
$tree = $tree->{$key};
}
}
return $tree; } |
sub libPath
{ my $self = shift;
my @data = caller;
return $data[1]; } |
sub libRootPath
{ my $self = shift;
my $path = libPath();
$path =~ s/.Pdoc.Config.pm$//;
return $path; } |
sub load
{ my $self = shift;
my $fname = shift;
unless ($fname) {
cluck("Pass a configuration file name as argument!\n");
return 0;
}
my $fpt = FileHandle->new($fname);
unless ($fpt) {
cluck("Failed to open file $fname\n");
return 0;
}
my $line;
while (defined ($line = <$fpt>)) {
chomp $line;
my @list = ();
while ($line =~ /\[([^\]]+)\](.*)/) {
push(@list, $1);
$line = $2;
}
$self->setVal(@list);
}
$fpt->close();
$self->loaded(1);
return 1; } |
sub loaded
{ my $self = shift;
my $val = shift;
$self->{'_loaded'} = $val if ($val);
return $self->{'_loaded'}; } |
sub new
{ my $class = shift;
my $self = {};
bless $self, $class;
$self->_init();
return $self; } |
sub save
{ my $self = shift;
my $fname = shift;
unless ($fname) {
cluck("Pass a configuration file name as argument!\n");
return 0;
}
my $fpt = FileHandle->new(">$fname");
unless ($fpt) {
cluck("Failed to create file $fname\n");
return 0;
}
print $fpt '# Configuration stored on ', localtime(), "\n";
my $key;
my $val;
my $hash = $self->{'_config'};
while (($key,$val) = each (%{$hash})) {
print $fpt '[', $key, ']';
if (ref($val) eq 'HASH') {
$hash = $val;
} else {
print $fpt '[', $val, ']', "\n";
}
}
$fpt->close();
return 1; } |
sub setVal
{ my $self = shift;
my @list = @_;
my $entry = {};
if (scalar(@list) == 1) {
$self->{'_config'}->{$list[0]} = 1;
} else {
my $val = pop(@list);
my $top = shift(@list);
while (my $key = pop(@list)) {
my $hash = {};
$hash->{$key} = $val;
$val = $hash;
}
$self->{'_config'}->{$top} = $val;
} } |
General documentation
Copyright (c) 2001 by Raphael Leplae (
raphael@scmbb.ulb.ac.be). All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.