blib/lib/Db/Mediasurface/ReadConfig.pm

filter: Statementstmt 29.8% 17 / 57 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 25.2% 23 / 91 CC 12 SCAR 32.3
CC
12
Cov
22%
_parse_config
57.3
new
12.0
AUTOLOAD
8.8
CRAP 25.2
↑ Summary
1 package Db::Mediasurface::ReadConfig;
2 1 $VERSION = '0.01';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new
8 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
9 0 my ($class,%arg) = @_;
10 my $self = {
11 0 ▶ _config_file => $arg{path} || croak("need path to config file"),
Condition0%0/2
$arg{'path'} || croak('need path to config file')
l!l
00
MC/DC0%0/1
$arg{'path'} || croak('need path to config file')
$arg{'path'}
Truth table0%0/2
A
A= $arg{'path'}
Aresult
11
00
12 _config_tree => undef
13 };
14 0 ▶ croak("config file does not exist") unless -T $self->{_config_file};
Branch: 1 branch
BranchTrueFalseTotal
unless -T $self->{"_config_file"} 0 0 0
15 0 my $obj = bless $self, $class;
16 0 $obj->_parse_config;
17 0 return $obj;
18 }
19  
20 sub _parse_config
21 0 ▶{
Subroutine _parse_config: not called CC=8 SCAR=40.5
22 0 my $self = $_[0];
23 0 ▶ return if defined $self->{_config_tree};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_config_tree"} 0 0 0
24 1 ▶ use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 sysopen(CONFIG, $self->{_config_file}, O_RDONLY)
26 0 ▶ or croak("Couldn't open ".$self->{_config_file}." for reading: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
sysopen CONFIG, $self->{"_config_file"}, 0 or croak(q[Couldn't open ] . $self->{"_config_file"} . " for reading: $!\n") 0 0 0
27 0 ▶ while (my $line = <CONFIG>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline CONFIG)) 0 0 0
28 0 chomp($line);
29 0 $line =~ s/#.*$//;
30 0 my ($key,$value) = split /=/, $line;
31 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
32 0 $key =~ s/\s//g;
33 0 $key =~ s/\./_/g;
34 0 ▶ if ($value =~ /"(.*?)"/){
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /"(.*?)"/) { } 0 0 0
elsif ($value =~ /(\d+)/) { } 0 0 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;
42 0 my $method = "set_$key";
43 0 $self->$method($value);
44 }
45 close(CONFIG)
46 0 ▶ or croak("Couldn't close ".$self->{_config_file}.": $!\n");
Branch: 1 branch
BranchTrueFalseTotal
close CONFIG or croak(q[Couldn't close ] . $self->{"_config_file"} . ": $!\n") 0 0 0
47 }
48  
49 sub AUTOLOAD
50 0 ▶{
Subroutine AUTOLOAD: not called CC=3 SCAR=21.7
51 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
52 0 my ($self, $newval) = @_;
53 0 ▶ if ($AUTOLOAD =~ /.*::get_([a-zA-Z0-9_]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($Db::Mediasurface::ReadConfig::AUTOLOAD =~ /.*::get_([a-zA-Z0-9_]+)/) 0 0 0
54 {
55 0 my $attr_name = $1;
56 0 ▶ *{$AUTOLOAD} = sub { return $_[0]->{_config_tree}->{$attr_name} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 return $self->{_config_tree}->{$attr_name};
58 }
59 0 ▶ if ($AUTOLOAD =~ /.*::set_([a-zA-Z0-9_]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($Db::Mediasurface::ReadConfig::AUTOLOAD =~ /.*::set_([a-zA-Z0-9_]+)/) 0 0 0
60 {
61 0 my $attr_name = $1;
62 0 ▶ *{$AUTOLOAD} = sub { $_[0]->{_config_tree}->{$attr_name} = $_[1]; return; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 $self->{_config_tree}->{$attr_name} = $newval;
64 0 return;
65 }
66 }
67 1 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  
↑ Summary