line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::CmdRC; |
2
|
7
|
|
|
7
|
|
226315
|
use strict; |
|
7
|
|
|
|
|
22
|
|
|
7
|
|
|
|
|
423
|
|
3
|
7
|
|
|
7
|
|
39
|
use warnings; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
207
|
|
4
|
7
|
|
|
7
|
|
40
|
use File::Spec; |
|
7
|
|
|
|
|
105
|
|
|
7
|
|
|
|
|
179
|
|
5
|
7
|
|
|
7
|
|
12501
|
use Config::Simple; |
|
7
|
|
|
|
|
127666
|
|
|
7
|
|
|
|
|
89
|
|
6
|
7
|
|
|
7
|
|
7780
|
use Hash::Merge qw/merge/; |
|
7
|
|
|
|
|
33803
|
|
|
7
|
|
|
|
|
1314
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $RC; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $RC_DIRS = ($^O =~ m!Win32!) ? [ |
13
|
|
|
|
|
|
|
# TODO: better windows support |
14
|
|
|
|
|
|
|
'.', |
15
|
|
|
|
|
|
|
$ENV{CMDRC_DIR}, |
16
|
|
|
|
|
|
|
$ENV{HOME}, |
17
|
|
|
|
|
|
|
] : [ |
18
|
|
|
|
|
|
|
# TODO: Are these common configuration directories? |
19
|
|
|
|
|
|
|
'.', |
20
|
|
|
|
|
|
|
$ENV{CMDRC_DIR}, |
21
|
|
|
|
|
|
|
$ENV{HOME}, |
22
|
|
|
|
|
|
|
'/etc', |
23
|
|
|
|
|
|
|
]; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub import { |
26
|
6
|
|
|
6
|
|
69
|
my $class = shift; |
27
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
15
|
my $caller_class = caller; |
29
|
|
|
|
|
|
|
|
30
|
6
|
|
|
|
|
28
|
$class->read(@_); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
{ |
33
|
7
|
|
|
7
|
|
57
|
no strict 'refs'; ## no critic |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
3723
|
|
|
6
|
|
|
|
|
11
|
|
34
|
6
|
|
|
|
|
15
|
*{"${caller_class}::RC"} = \&RC; |
|
6
|
|
|
|
|
8823
|
|
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub read { |
39
|
8
|
|
|
8
|
1
|
1224
|
my $self = shift; |
40
|
|
|
|
|
|
|
|
41
|
8
|
|
|
|
|
38
|
my ($file, $dir, $loader) = _get_args(@_); |
42
|
|
|
|
|
|
|
|
43
|
8
|
100
|
|
|
|
23
|
if ( my $user_rc_file = _get_user_rcfile() ) { |
44
|
1
|
|
|
|
|
2
|
$file = $user_rc_file; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
8
|
|
|
|
|
13
|
my %hash; |
48
|
8
|
|
|
|
|
15
|
for my $d ( @{_array($dir)}, @{$RC_DIRS} ) { |
|
8
|
|
|
|
|
31
|
|
|
8
|
|
|
|
|
25
|
|
49
|
38
|
100
|
|
|
|
9848
|
next unless $d; |
50
|
26
|
|
|
|
|
32
|
for my $f ( @{_array($file)} ) { |
|
26
|
|
|
|
|
51
|
|
51
|
25
|
|
|
|
|
6327
|
my $path = File::Spec->catfile($d, $f); |
52
|
25
|
100
|
|
|
|
477
|
if (-e $path) { |
53
|
7
|
|
|
|
|
21
|
my $config = $loader->($path); |
54
|
7
|
|
|
|
|
21656
|
%hash = %{ merge(\%hash, $config) }; |
|
7
|
|
|
|
|
36
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
8
|
|
|
|
|
54
|
return $RC = \%hash; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _get_user_rcfile { |
63
|
8
|
100
|
|
8
|
|
54
|
return unless @ARGV; |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
1
|
my $f = 0; |
66
|
1
|
|
|
|
|
2
|
my @rcfiles; |
67
|
1
|
|
|
|
|
2
|
for my $opt (@ARGV) { |
68
|
2
|
100
|
66
|
|
|
17
|
if ($f && $opt) { |
|
|
50
|
33
|
|
|
|
|
69
|
1
|
|
|
|
|
1
|
push @rcfiles, $opt; |
70
|
1
|
|
|
|
|
3
|
$f = 0; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
elsif ($opt && $opt eq '--rc') { |
73
|
1
|
|
|
|
|
2
|
$f = 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
6
|
return \@rcfiles if @rcfiles; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _get_args { |
81
|
8
|
|
|
8
|
|
78
|
my ($file, $dir, $loader); |
82
|
8
|
100
|
|
|
|
45
|
if (@_ == 1) { |
83
|
4
|
|
|
|
|
11
|
$file = shift; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
4
|
|
|
|
|
12
|
my %arg = @_; |
87
|
4
|
|
100
|
|
|
28
|
$file = $arg{file} || []; # TODO: Is default file name needed? |
88
|
4
|
|
100
|
|
|
23
|
$dir = $arg{dir} || []; |
89
|
4
|
|
|
|
|
11
|
$loader = $arg{loader}; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
$loader = $loader ? $loader : sub { |
93
|
6
|
|
|
6
|
|
11
|
my $path = shift; |
94
|
6
|
|
|
|
|
47
|
my $config = Config::Simple->new($path); |
95
|
6
|
|
|
|
|
4929
|
return $config->vars; |
96
|
8
|
100
|
|
|
|
46
|
}; |
97
|
|
|
|
|
|
|
|
98
|
8
|
|
|
|
|
29
|
return($file, $dir, $loader); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _array { |
102
|
34
|
|
|
34
|
|
51
|
my $v = shift; |
103
|
34
|
100
|
|
|
|
129
|
return (ref $v eq 'ARRAY') ? $v : [$v]; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
10
|
|
|
10
|
1
|
96
|
sub RC { $RC } |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |