| 1 | package XML::Conf; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: Conf.pm,v 1.6 2003/12/14 20:44:11 jonasbn Exp $ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Tie::DeepTied; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | $VERSION = 0.06; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||
| 16 | 4 | ▶ | my ($class, $filename, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 4 | my $xml; | |||||||||||||||||||||||||||
| 18 | my $fn; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | if (ref($filename) eq 'SCALAR') { #Internal use (TIEHASH) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 21 | 0 | $xml = $$filename; | |||||||||||||||||||||||||||
| 22 | } elsif ($filename =~ m/^\s*\<.*\>\s*$/s) { #internal use (TIEHASH) | ||||||||||||||||||||||||||||
| 23 | 0 | $xml = $filename; | |||||||||||||||||||||||||||
| 24 | } else { #internal use (ReadConfig) | ||||||||||||||||||||||||||||
| 25 | 4 | ▶ | $filename = "./$filename" if ($filename !~ /^[\/\.]/ && -e "./$filename"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $filename =~ m[^[/\.]] and -e "./$filename"
MC/DC50%1/2
not $filename =~ m[^[/\.]] and -e "./$filename"
not $filename =~ m[^[/\.]]
-e "./$filename"
Truth table66%2/3
not(A) and B
A= not $filename =~ m[^[/\.]]B= -e "./$filename"
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | open(I, $filename) || croak "Could not open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 4 | $xml = join("", <I>); | |||||||||||||||||||||||||||
| 28 | 4 | close(I); | |||||||||||||||||||||||||||
| 29 | 4 | $fn = $filename; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | my $hash = XML::Simple::XMLin($xml) || return undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
XML::Simple::XMLin($xml) || (return undef)
MC/DC0%0/1
XML::Simple::XMLin($xml) || (return undef)
XML::Simple::XMLin($xml)
Truth table50%1/2
A
A= XML::Simple::XMLin($xml)
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $case = $opts{'case'}?$opts{'case'}:'_dummysub'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | #my $case = $opts{'case'}; | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | $hash = &_trans($hash, eval "sub { $case(\$_);} ") if ($case); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | #$hash = &_trans($hash, $case) if ($case); | ||||||||||||||||||||||||||||
| 36 | 4 | my $self = {'data' => $hash, 'case' => $case, 'fn' => $fn}; | |||||||||||||||||||||||||||
| 37 | 4 | my $sig = $opts{'sig'}; | |||||||||||||||||||||||||||
| 38 | 4 | ▶ | if ($sig) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $SIG{$sig} = sub { $self->ReadConfig; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 4 | bless $self, $class; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub _dummysub { | ||||||||||||||||||||||||||||
| 45 | 66 | ▶ | my $val = shift; | ||||||||||||||||||||||||||
|
Subroutine
_dummysub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub _trans { | ||||||||||||||||||||||||||||
| 49 | 64 | ▶ | my ($tree, $case) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trans: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 50 | 64 | ▶ | return $tree unless (UNIVERSAL::isa($tree, 'HASH')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 20 | my %hash; | |||||||||||||||||||||||||||
| 52 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 20 | foreach (keys %$tree) { | |||||||||||||||||||||||||||
| 54 | 60 | $hash{ &$case($_) } = &_trans($tree->{$_}, $case); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 3 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 20 | \%hash; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub _val { | ||||||||||||||||||||||||||||
| 61 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_val: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 62 | 8 | my $data = $self->{'data'}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | foreach (@_) { | |||||||||||||||||||||||||||
| 65 | 8 | $data = $data->{$_}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 8 | ▶ | wantarray ? split("\n", $data) : $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub _setval { | ||||||||||||||||||||||||||||
| 71 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_setval: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 3 | my $data = \$self->{'data'}; | |||||||||||||||||||||||||||
| 73 | 3 | ▶ | while (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 5 | $data = \($$data->{shift()}); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 3 | $$data = shift; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub _newval { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_newval: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | $self->_setval(@_); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub _delval { | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_delval: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 3 | my $data = $self->{'data'}; | |||||||||||||||||||||||||||
| 87 | 3 | ▶ | while (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | $data = $data->{shift()}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 3 | delete $data->{shift()}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub ReadConfig { | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
ReadConfig: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 1 | my $fn = $self->{'fn'}; | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | return undef unless ($fn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | my $new = &new(__PACKAGE__, $fn, 'case' => $self->{'case'}); | |||||||||||||||||||||||||||
| 98 | 1 | %$self = %$new; | |||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub Sections { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
Sections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 1 | $self->Parameters(@_); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub Parameters { | ||||||||||||||||||||||||||||
| 108 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
Parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 2 | my $val = $self->_val(@_); | |||||||||||||||||||||||||||
| 110 | 2 | my $case = $self->{'case'}; | |||||||||||||||||||||||||||
| 111 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 2 | map { &$case($_); } keys %$val; | |||||||||||||||||||||||||||
| 113 | 3 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub RewriteConfig { | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
RewriteConfig: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 1 | my $fn = $self->{'fn'}; | |||||||||||||||||||||||||||
| 119 | 1 | ▶ | croak "No filename" unless ($fn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | $self->WriteConfig($fn); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub WriteConfig { | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
WriteConfig: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 2 | my $xml = XMLout($self->{'data'}, xmldecl => 1); | |||||||||||||||||||||||||||
| 126 | 2 | ▶ | open(O, ">$name") || croak "Can't rewrite $name: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 2 | print O $xml; | |||||||||||||||||||||||||||
| 128 | 2 | close(O); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub TIEHASH { | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | 0 | $class->new(@_); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub FETCH { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 138 | 0 | my $val = $self->_val($key); | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (UNIVERSAL::isa($val, 'HASH') && !tied(%$val)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
UNIVERSAL::isa($val, "HASH") and not tied %$val
MC/DC0%0/2
UNIVERSAL::isa($val, "HASH") and not tied %$val
UNIVERSAL::isa($val, "HASH")
not tied %$val
Truth table0%0/3
A and not(B)
A= UNIVERSAL::isa($val, "HASH")B= not tied %$val
| |||||||||||||||||||||||||||||
| 140 | 0 | my %h = %$val; | |||||||||||||||||||||||||||
| 141 | 0 | tie %$val, 'Tie::StdHash', $self, $key; | |||||||||||||||||||||||||||
| 142 | 0 | %$val = %h; | |||||||||||||||||||||||||||
| 143 | 0 | tie %$val, 'Tie::DeepTied', $self, $key; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | $val; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub STORE { | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my ($self, $key, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 150 | 0 | $self->_setval($key, $val); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub DELETE { | ||||||||||||||||||||||||||||
| 154 | 1 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 1 | $self->_delval($key); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub CLEAR { | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 1 | $self->{'data'} = {}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub EXISTS { | ||||||||||||||||||||||||||||
| 164 | 2 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | 2 | exists $self->{'data'}->{$key}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | sub FIRSTKEY { | ||||||||||||||||||||||||||||
| 169 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 3 | keys %{$self->{'data'}}; | |||||||||||||||||||||||||||
| 171 | 3 | each %{$self->{'data'}}; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub NEXTKEY { | ||||||||||||||||||||||||||||
| 175 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 176 | 4 | each %{$self->{'data'}}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 3 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||