File Coverage

blib/lib/Db/Mediasurface/ReadConfig.pm
Criterion Covered Total %
statement 15 55 27.2
branch 0 18 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 1 1 100.0
total 21 87 24.1


line stmt bran cond sub pod time code
1             package Db::Mediasurface::ReadConfig;
2             $VERSION = '0.01';
3 1     1   595 use strict;
  1         1  
  1         31  
4 1     1   5 use vars qw( $AUTOLOAD );
  1         1  
  1         47  
5 1     1   5 use Carp;
  1         4  
  1         216  
6              
7             sub new
8             {
9 0     0 1   my ($class,%arg) = @_;
10 0   0       my $self = {
11             _config_file => $arg{path} || croak("need path to config file"),
12             _config_tree => undef
13             };
14 0 0         croak("config file does not exist") unless -T $self->{_config_file};
15 0           my $obj = bless $self, $class;
16 0           $obj->_parse_config;
17 0           return $obj;
18             }
19              
20             sub _parse_config
21             {
22 0     0     my $self = $_[0];
23 0 0         return if defined $self->{_config_tree};
24 1     1   5 use Fcntl;
  1         2  
  1         669  
25 0 0         sysopen(CONFIG, $self->{_config_file}, O_RDONLY)
26             or croak("Couldn't open ".$self->{_config_file}." for reading: $!\n");
27 0           while (my $line = ){
28 0           chomp($line);
29 0           $line =~ s/#.*$//;
30 0           my ($key,$value) = split /=/, $line;
31 0 0         next unless defined $value;
32 0           $key =~ s/\s//g;
33 0           $key =~ s/\./_/g;
34 0 0         if ($value =~ /"(.*?)"/){
    0          
35 0           $value = $1;
36             } elsif ($value =~ /(\d+)/) {
37 0           $value = $1;
38             } else {
39 0           $value =~ s/\s//g;
40             }
41 0           $value =~ s/([^\\]|^)\$([a-zA-Z_][a-zA-Z0-9_]*)/$self->{_config_tree}->{$2}/eg;
  0            
42 0           my $method = "set_$key";
43 0           $self->$method($value);
44             }
45 0 0         close(CONFIG)
46             or croak("Couldn't close ".$self->{_config_file}.": $!\n");
47             }
48              
49             sub AUTOLOAD
50             {
51 1     1   5 no strict "refs";
  1         1  
  1         230  
52 0     0     my ($self, $newval) = @_;
53 0 0         if ($AUTOLOAD =~ /.*::get_([a-zA-Z0-9_]+)/)
54             {
55 0           my $attr_name = $1;
56 0     0     *{$AUTOLOAD} = sub { return $_[0]->{_config_tree}->{$attr_name} };
  0            
  0            
57 0           return $self->{_config_tree}->{$attr_name};
58             }
59 0 0         if ($AUTOLOAD =~ /.*::set_([a-zA-Z0-9_]+)/)
60             {
61 0           my $attr_name = $1;
62 0     0     *{$AUTOLOAD} = sub { $_[0]->{_config_tree}->{$attr_name} = $_[1]; return; };
  0            
  0            
  0            
63 0           $self->{_config_tree}->{$attr_name} = $newval;
64 0           return;
65             }
66             }
67             1;
68              
69             =head1 NAME
70              
71             Db::Mediasurface::ReadConfig - reads, parses, and stores configuration from a Mediasurface ms.properties file.
72              
73             =head1 VERSION
74              
75             This document refers to version 0.01 of DB::Mediasurface::ReadConfig, released August 3, 2001.
76              
77             =head1 SYNOPSIS
78              
79             use Db::Mediasurface::ReadConfig;
80              
81             my $path = '/opt/ms/3.0.2/etc/ms.properties';
82              
83             my $config = Db::Mediasurface::ReadConfig->new( path=>$path );
84              
85             print('oracle user is '.$config->get_username."\n");
86              
87             =head1 DESCRIPTION
88              
89             =head2 Overview
90              
91             When supplied with a path to a Mediasurface configuration file (usually called ms.properties), this module loads the configuration details into an object, which can be interrogated at a later time.
92              
93             =head2 Constructor
94              
95             =over 4
96              
97             =item $config = Db::Mediasurface::ReadConfig->new( path=>$path );
98              
99             This class method constructs a new configuration object by reading the file at location $path. This module assumes that the configuration file is constructed in the following manner...
100              
101             Each key/value pair occurs on a single line, and the key and value are separated by an equals sign (=) and (optionally) white space. Any text on a line is ignored which follows a hash symbol (#).
102              
103             =back
104              
105             =head2 Methods
106              
107             =over 4
108              
109             =item $value = $config->get_SOME_CONFIG_KEY;
110              
111             Returns the value associated with a particular configuration key. If the key contained a full-stop in the config file, this is replaced by an underscore. For example, to get the value associated with jdbc.driver in the config file, use $config->get_jdbc_driver
112              
113             =item $config->set_SOME_CONFIG_KEY($value);
114              
115             Sets the value of a given key. Does not write the value back to the config file - no permanent damage is done ;)
116              
117             =back
118              
119             =head1 AUTHOR
120              
121             Nigel Wetters (nwetters@cpan.org)
122              
123             =head1 COPYRIGHT
124              
125             Copyright (c) 2001, Nigel Wetters. All Rights Reserved.
126             This module is free software. It may be used, redistributed
127             and/or modified under the same terms as Perl itself.
128