NAME
Properties - Module for option file parsing
VERSION
1.3
SYNOPSIS
use Properties;
my $options = Properties->new('/etc/some_optionsfile');
if($options->error()) {
print("Errorcode: " . $options->get_errorCode() ."\n");
print("Error description: " . $options->get_errorDescription() ."\n");
$options->printStacktrace();
exit(0);
}
my $configHash = $options->getCompleteConfig();
if($options->error()) {
print("Errorcode: " . $options->get_errorCode() ."\n");
print("Error description: " . $options->get_errorDescription() ."\n");
$options->printStacktrace();
exit(0);
}
my $property = $options->getProperty("socket.remote.hostname");
if($options->error()) {
print("Errorcode: " . $options->get_errorCode() ."\n");
print("Error description: " . $options->get_errorDescription() ."\n");
$options->printStacktrace();
exit(0);
}
DESCRIPTION
Properties give you the ability to define program properties in an external file and parse them as needed. You can fetch the complete config in a well formed hash structure or fetch single properties. This class also need the Merror Module to indicate error states. Every method will set his internal Merror state to an error if something strange happend. You have to catch these error yourself via defined methods.
METHODS
- new(propertyFile)
-
Constructor.
Example: my $obj = Properties->new("/etc/myClient/socket.properties"); if(obj->error()) { print("Errorcode: " . $options->get_errorCode() ."\n"); print("Error description: " . $options->get_errorDescription() ."\n"); $options->printStacktrace(); exit(0); }
- getCompleteConfig
-
Returns the attribute value pairs defined in the properties file as an well formed anonymous hash. See HASHSTRUCTURE for detailed information about the retunred structure.
Example: my $c_hash = $obj->getCompleteConfig(); if($obj->error()) { print("Errorcode: " . $options->get_errorCode() ."\n"); print("Error description: " . $options->get_errorDescription() ."\n"); $options->printStacktrace(); exit(0); }
- getProperty(propertyName)
-
Searches propertyName in configured property file and returns the value of that property
Example: $obj->getProperty("socket.remote.hostname"); if($obj->error()) { print("Errorcode: " . $options->get_errorCode() ."\n"); print("Error description: " . $options->get_errorDescription() ."\n"); $options->printStacktrace(); exit(0); }
- error
-
Wraps the error function of Merror class to hide the internal represantation of Merror. If this method returns 1 an error happend.
- get_errorDescription
-
Returns the error description.
- get_errorCode
-
Returns the errorcode.
- printStacktrace
-
Prints out the stacktrace.
- get_stacktrace
-
Returns the stacktrace as an array where every element is one level of the stacktrace.
PROPERTYFILE
Lines starting with a # are ignored. The syntax of an key-value propertie pair is:
a.b.c.d = foo
Here you have a propertie named a.b.c.d
with an value of foo
. Properties are seen in groups. This mean that if you define another property:
a.b.c.e = bar
the level of d
and e
are the direct sublevel of c
when calling getCompleteConfig.
Example of an property file:
socket.local.port = 3333
socket.local.hostname = localhost
socket.local.maxconnections = 10
socket.remote.port = 3334
socket.remote.hostname = remotehost
HASHSTRUCTURE
When calling getCompleteConfig
the resulting hashstructure depends on the levels of different attributes. Every subitem of the anonymous hash stands for an subitem ob the attribute. Lets have a look at an example. Think about we are having a property file like the one described in section PROPERTYFILE. If we call method getCompleteConfig
the resulting hash will look like this:
$returned_hash =>
{socket} =>
{local} =>
{port} = 3333
{hostname} = localhost
{maxconnections} = 10
{remote} =>
{port} = 3334
{hostname} = remotehost
So, if you want to use the value of the local port you must access it via:
$$returned_hash{socket}{local}{port}
BUGS
Option value missparsing: If an option value contains a dot the value was also parsed as an parameter structure. Fixed in version 1.3. Thanks to <florent.lartet@univ-tlse2.fr>
ACKNOWLEDGEMENTS
If you find any bugs or got some feature you wish to have implemented please register at mantis.markus-mazurczak.de
.
COPYRIGHT
See README.
AVAILABILITY
You can allways get the latest version from CPAN.
AUTHOR
Markus Mazurczak <coding@markus-mazurczak.de>