line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Application::Plugin::Config::YAML; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
879
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
13
|
use vars qw($VERSION @EXPORT); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
6
|
1
|
|
|
1
|
|
639
|
use CGI::Application; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Config::YAML; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$VERSION = '0.01'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
@EXPORT = qw( |
12
|
|
|
|
|
|
|
config_file |
13
|
|
|
|
|
|
|
config_param |
14
|
|
|
|
|
|
|
config |
15
|
|
|
|
|
|
|
config_fold |
16
|
|
|
|
|
|
|
config_read |
17
|
|
|
|
|
|
|
get_hash |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub import { |
21
|
|
|
|
|
|
|
my $pkg = shift; |
22
|
|
|
|
|
|
|
my $call = caller; |
23
|
|
|
|
|
|
|
no strict 'refs'; |
24
|
|
|
|
|
|
|
foreach my $sym (@EXPORT) { |
25
|
|
|
|
|
|
|
*{"${call}::$sym"} = \&{$sym}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub config_file { |
30
|
|
|
|
|
|
|
my $self = shift; |
31
|
|
|
|
|
|
|
my $file_name = shift; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
if ( defined $file_name ) { |
34
|
|
|
|
|
|
|
$self->{__CONFIG_YAML}->{__FILE_NAME} = $file_name; |
35
|
|
|
|
|
|
|
$self->{__CONFIG_YAML}->{__FILE_CHANGED} = 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
|
|
|
|
|
|
$file_name = $self->{__CONFIG_YAML}->{__FILE_NAME}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
if ( ! defined $file_name ) { |
42
|
|
|
|
|
|
|
$ENV{CGIAPP_CONFIG_FILE} =~ /(.*)/; |
43
|
|
|
|
|
|
|
$file_name = $1; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$file_name; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub config_param { |
50
|
|
|
|
|
|
|
my $self = shift; |
51
|
|
|
|
|
|
|
my @params = @_; |
52
|
|
|
|
|
|
|
my $conf = $self->config(); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if ( scalar(@params) == 0 ) { |
55
|
|
|
|
|
|
|
return scalar($self->get_hash); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ( scalar(@params) == 1 ) { |
58
|
|
|
|
|
|
|
return $conf->get($params[0]); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
|
|
|
|
|
|
my %params = (@params); |
62
|
|
|
|
|
|
|
$conf->set($_ => $params{$_}) foreach (keys %params); |
63
|
|
|
|
|
|
|
if ( $conf->write ) { |
64
|
|
|
|
|
|
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
else{ |
67
|
|
|
|
|
|
|
die "Config-Plugin: Could not write to config file (" . $self->config_file . ")! "; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub config_fold { |
73
|
|
|
|
|
|
|
my ($self, $data) = @_; |
74
|
|
|
|
|
|
|
my $conf = $self->config(); |
75
|
|
|
|
|
|
|
$conf->fold($data); |
76
|
|
|
|
|
|
|
return; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub config_read { |
80
|
|
|
|
|
|
|
my ($self, $file) = @_; |
81
|
|
|
|
|
|
|
my $conf = $self->config(); |
82
|
|
|
|
|
|
|
$conf->read($file); |
83
|
|
|
|
|
|
|
return; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub config { |
87
|
|
|
|
|
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
my $create = !$self->{__CONFIG_YAML}->{__CONFIG_OBJ} || $self->{__CONFIG_YAML}->{__FILE_CHANGED}; |
89
|
|
|
|
|
|
|
if ( $create ) { |
90
|
|
|
|
|
|
|
my $file_name = $self->config_file or die "No config file specified!"; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $conf; |
93
|
|
|
|
|
|
|
eval{ |
94
|
|
|
|
|
|
|
$conf = Config::YAML->new(config => $file_name); |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
die "Could not create Config::YAML object for file $file_name! $@" if $@; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$self->{__CONFIG_YAML}->{__CONFIG_OBJ} = $conf; |
100
|
|
|
|
|
|
|
$self->{__CONFIG_YAML}->{__FILE_CHANGED} = 0; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
return $self->{__CONFIG_YAML}->{__CONFIG_OBJ}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub get_hash { |
107
|
|
|
|
|
|
|
my $self = shift; |
108
|
|
|
|
|
|
|
my $yaml; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
open(FH,'<',$self->config_file) or die "Can't open $self->config_file; $!\n"; |
111
|
|
|
|
|
|
|
while (my $line = ) { |
112
|
|
|
|
|
|
|
next if ($line =~ /^\-{3,}/); |
113
|
|
|
|
|
|
|
next if ($line =~ /^#/); |
114
|
|
|
|
|
|
|
next if ($line =~ /^$/); |
115
|
|
|
|
|
|
|
$yaml .= $line; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
close(FH); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $tmpyaml = YAML::Load($yaml); |
120
|
|
|
|
|
|
|
return $tmpyaml; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |