line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (c) 2005 Timothy Appnel |
2
|
|
|
|
|
|
|
# http://www.timaoutloud.org/ |
3
|
|
|
|
|
|
|
# This code is released under the Artistic License. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Class::ConfigMgr - A base class for implementing a |
6
|
|
|
|
|
|
|
# singleton object configuration manager. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Class::ConfigMgr; |
10
|
1
|
|
|
1
|
|
1251
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use vars qw( $VERSION ); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
89
|
|
14
|
|
|
|
|
|
|
$VERSION = 0.1; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
1859
|
use Class::ErrorHandler; |
|
1
|
|
|
|
|
303
|
|
|
1
|
|
|
|
|
88
|
|
17
|
|
|
|
|
|
|
@Class::ConfigMgr::ISA = qw( Class::ErrorHandler ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub instance { |
20
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
21
|
1
|
|
|
1
|
|
9
|
no strict 'refs'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
896
|
|
22
|
0
|
0
|
|
|
|
|
return ${"${class}::cfg"} if ${"${class}::cfg"}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
${"${class}::cfg"} = $class->new; |
|
0
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub new { |
27
|
0
|
|
|
0
|
0
|
|
my $mgr = bless { __directive => {} }, $_[0]; |
28
|
0
|
|
|
|
|
|
$mgr->init; |
29
|
0
|
|
|
|
|
|
$mgr; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
0
|
1
|
|
sub init { die "'init' must be overloaded." } |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub define { |
35
|
0
|
|
|
0
|
1
|
|
my $mgr = shift; |
36
|
0
|
|
|
|
|
|
my ( $dir, %param ) = @_; |
37
|
0
|
|
|
|
|
|
$mgr->{__directive}{$dir} = undef; |
38
|
0
|
0
|
|
|
|
|
$mgr->set( $dir, $param{Default} ) if exists $param{Default}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub read_config { |
42
|
0
|
|
|
0
|
1
|
|
my ( $class, $cfg_file ) = @_; |
43
|
0
|
|
|
|
|
|
my $mgr = $class->instance; |
44
|
0
|
|
|
|
|
|
local ( *FH, $_, $/ ); |
45
|
0
|
|
|
|
|
|
$/ = "\n"; |
46
|
0
|
0
|
|
|
|
|
open FH, $cfg_file |
47
|
|
|
|
|
|
|
or return $class->error("Error opening file '$cfg_file': $!"); |
48
|
0
|
|
|
|
|
|
while () { |
49
|
0
|
|
|
|
|
|
chomp; |
50
|
0
|
0
|
0
|
|
|
|
next if !/\S/ || /^#/; |
51
|
0
|
|
|
|
|
|
my ( $dir, $val ) = $_ =~ /^\s*(\S+)\s+(.+)$/; |
52
|
0
|
|
|
|
|
|
$val =~ s/\s*$//; |
53
|
0
|
0
|
0
|
|
|
|
next unless $dir && defined($val); |
54
|
0
|
0
|
|
|
|
|
return $class->error("$cfg_file: $.: directive $dir") |
55
|
|
|
|
|
|
|
unless exists $mgr->{__directive}->{$dir}; |
56
|
0
|
|
|
|
|
|
$mgr->set( $dir, $val ); |
57
|
|
|
|
|
|
|
} |
58
|
0
|
|
|
|
|
|
close FH; |
59
|
0
|
|
|
|
|
|
1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
0
|
0
|
|
sub get { $_[0]->{__directive}{ $_[1] } } |
63
|
0
|
|
|
0
|
0
|
|
sub set { $_[0]->{__directive}{ $_[1] } = $_[2] } |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
0
|
|
|
sub DESTROY { } |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
1
|
|
6
|
use vars qw( $AUTOLOAD ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
132
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub AUTOLOAD { |
70
|
0
|
|
|
0
|
|
|
my $mgr = $_[0]; |
71
|
0
|
|
|
|
|
|
( my $dir = $AUTOLOAD ) =~ s!.+::!!; |
72
|
0
|
0
|
|
|
|
|
die("No such configuration directive '$dir'") |
73
|
|
|
|
|
|
|
unless exists $mgr->{__directive}->{$dir}; |
74
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
132
|
|
75
|
|
|
|
|
|
|
*$AUTOLOAD = sub { |
76
|
0
|
|
|
0
|
|
|
my $mgr = shift; |
77
|
0
|
0
|
|
|
|
|
@_ ? $mgr->set( $dir, $_[0] ) : $mgr->get($dir); |
78
|
0
|
|
|
|
|
|
}; |
79
|
0
|
|
|
|
|
|
goto &$AUTOLOAD; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__END__ |