line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Rad::Plugin::ValuePriority; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
45438
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1396
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
App::Rad::Plugin::ValuePriority - A Plugin to make it easy to get value from all acessors. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.02 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 Snippet |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use App::Rad qw/ValuePriority/; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub command_1 { |
19
|
|
|
|
|
|
|
my $c = shift; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$c->stash->{value_1} = "Test 01"; |
22
|
|
|
|
|
|
|
$c->default_value->{value_2} = "Test 02"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
return join " --- ", $c->value->{value_1}, $c->value->{value_2}, $c->value->{value_3}; |
25
|
|
|
|
|
|
|
# It will print Test 01 --- Test 02 --- |
26
|
|
|
|
|
|
|
# but if you call program like this: |
27
|
|
|
|
|
|
|
# ./my_app.pl command_1 --value_2="Option 02" --value_3="Option 03" |
28
|
|
|
|
|
|
|
# it will print: |
29
|
|
|
|
|
|
|
# Test 01 --- Option 02 --- Option 03 |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub command_2 { |
33
|
|
|
|
|
|
|
my $c = shift; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$c->stash->{value_1} = "Test 01"; |
36
|
|
|
|
|
|
|
$c->default_value->{value_2} = "Test 02"; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$c->to_stash; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return join " --- ", $c->stash->{value_1}, $c->stash->{value_2}, $c->stash->{value_3}; |
41
|
|
|
|
|
|
|
# It will print Test 01 --- Test 02 --- |
42
|
|
|
|
|
|
|
# but if you call program like this: |
43
|
|
|
|
|
|
|
# ./my_app.pl command_2 --value_2="Option 02" --value_3="Option 03" |
44
|
|
|
|
|
|
|
# it will print: |
45
|
|
|
|
|
|
|
# Test 01 --- Option 02 --- Option 03 |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 Methods |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 $c->load() |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Internal func |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub load { |
61
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
62
|
0
|
|
|
|
|
|
$c->set_priority(qw/options config stash default_value/); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 $c->default_value() |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
It is a acessor. You use it to set and get some key/value pairs. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub default_value { |
72
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
73
|
0
|
0
|
|
|
|
|
$c->{default_value}->{default_value} = {} unless exists $c->{default_value}->{default_value}; |
74
|
0
|
|
|
|
|
|
$c->{default_value}->{default_value}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 $c->set_priority() |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
It receives a ordered list of what should receive priority. |
80
|
|
|
|
|
|
|
The options are: options, config, stash, default_value |
81
|
|
|
|
|
|
|
And that is the default order. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub set_priority { |
86
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
87
|
0
|
|
|
|
|
|
my @prio = @_; |
88
|
0
|
|
|
|
|
|
my @nprio; |
89
|
0
|
|
|
|
|
|
die((join ", ", @nprio), " are not recognized.$/") |
90
|
0
|
0
|
|
|
|
|
if scalar (@nprio = grep {not m/^(?:options|config|stash|default_value)$/} @prio); |
91
|
0
|
|
|
|
|
|
$c->{default_value}->{priority} = [@prio]; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 $c->get_priority() |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
As the name says, it return the priority order. As a arrayref |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub get_priority { |
102
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
103
|
0
|
0
|
|
|
|
|
$c->load if not exists $c->{default_value}; |
104
|
0
|
|
|
|
|
|
$c->{default_value}->{priority}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 $c->to_stash() |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
it populate the $c->stash with the values obeying the setted order. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub to_stash { |
114
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
115
|
0
|
|
|
|
|
|
for my $key (keys %{ $c->value }) { |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$c->stash->{$key} = $c->value->{$key} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 $c->value() |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Return the value obeying the setted order. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub value { |
127
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
128
|
0
|
|
|
|
|
|
my $redo = shift; |
129
|
0
|
|
|
|
|
|
my $ret; |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
0
|
|
|
|
$c->load if not exists $c->{default_value} or not exists $c->{default_value}->{"values"}; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
for my $func (@{ $c->{default_value}->{priority} }) { |
|
0
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $turn = $c->$func; |
135
|
0
|
|
|
|
|
|
for my $key (keys %$turn) { |
136
|
0
|
0
|
|
|
|
|
next if exists $ret->{$key};# and defined $c->stash->{$key}; |
137
|
0
|
0
|
|
|
|
|
$ret->{$key} = $turn->{$key} if exists $turn->{$key}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
$c->stash->{default_value}->{"values"} = $ret; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
42 |