line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ============================================================================ |
2
|
|
|
|
|
|
|
package MooseX::App::Message::Envelope; |
3
|
|
|
|
|
|
|
# ============================================================================ |
4
|
|
|
|
|
|
|
|
5
|
15
|
|
|
15
|
|
418
|
use 5.010; |
|
15
|
|
|
|
|
62
|
|
6
|
15
|
|
|
15
|
|
96
|
use utf8; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
135
|
|
7
|
|
|
|
|
|
|
|
8
|
15
|
|
|
15
|
|
422
|
use namespace::autoclean; |
|
15
|
|
|
|
|
33
|
|
|
15
|
|
|
|
|
127
|
|
9
|
15
|
|
|
15
|
|
1559
|
use Moose; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
115
|
|
10
|
|
|
|
|
|
|
|
11
|
15
|
|
|
15
|
|
116385
|
use MooseX::App::Message::Block; |
|
15
|
|
|
|
|
57
|
|
|
15
|
|
|
|
|
710
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use overload |
14
|
15
|
|
|
15
|
|
130
|
'""' => "overload"; |
|
15
|
|
|
|
|
35
|
|
|
15
|
|
|
|
|
84
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has 'blocks' => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
isa => 'ArrayRef[MooseX::App::Message::Block]', |
19
|
|
|
|
|
|
|
traits => ['Array'], |
20
|
|
|
|
|
|
|
handles => { |
21
|
|
|
|
|
|
|
add_block => 'push', |
22
|
|
|
|
|
|
|
list_blocks => 'elements', |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'exitcode' => ( |
27
|
|
|
|
|
|
|
is => 'ro', |
28
|
|
|
|
|
|
|
isa => 'Int', |
29
|
|
|
|
|
|
|
predicate => 'has_exitcode', |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
around 'BUILDARGS' => sub { |
33
|
|
|
|
|
|
|
my $orig = shift; |
34
|
|
|
|
|
|
|
my $self = shift; |
35
|
|
|
|
|
|
|
my @args = @_; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $params; |
38
|
|
|
|
|
|
|
if (scalar @args == 1 |
39
|
|
|
|
|
|
|
&& ref($args[0]) eq 'HASH') { |
40
|
|
|
|
|
|
|
$params = $args[0]; |
41
|
|
|
|
|
|
|
} else { |
42
|
|
|
|
|
|
|
$params = { |
43
|
|
|
|
|
|
|
blocks => [], |
44
|
|
|
|
|
|
|
}; |
45
|
|
|
|
|
|
|
my @blocks; |
46
|
|
|
|
|
|
|
foreach my $element (@args) { |
47
|
|
|
|
|
|
|
next |
48
|
|
|
|
|
|
|
unless defined $element; |
49
|
|
|
|
|
|
|
if (blessed $element |
50
|
|
|
|
|
|
|
&& $element->isa('MooseX::App::Message::Block')) { |
51
|
|
|
|
|
|
|
push(@{$params->{blocks}},$element); |
52
|
|
|
|
|
|
|
} elsif ($element =~ /^\d+$/ |
53
|
|
|
|
|
|
|
&& $element <= 255 |
54
|
|
|
|
|
|
|
&& $element >= 0) { |
55
|
|
|
|
|
|
|
$params->{exitcode} = $element; |
56
|
|
|
|
|
|
|
} else { |
57
|
|
|
|
|
|
|
push(@{$params->{blocks}},MooseX::App::Message::Block->new( |
58
|
|
|
|
|
|
|
header => $element, |
59
|
|
|
|
|
|
|
)); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $self->$orig($params); |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub overload { |
68
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
0
|
if ($self->has_exitcode) { |
71
|
0
|
|
|
|
|
0
|
my $exitcode = $self->exitcode; |
72
|
0
|
0
|
|
|
|
0
|
if ($exitcode == 0) { |
73
|
0
|
|
|
|
|
0
|
print $self->stringify; |
74
|
|
|
|
|
|
|
} else { |
75
|
0
|
|
|
|
|
0
|
print STDERR $self->stringify; |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
0
|
exit $exitcode; |
78
|
|
|
|
|
|
|
} else { |
79
|
0
|
|
|
|
|
0
|
print $self->stringify; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
|
|
|
|
0
|
return; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub stringify { |
85
|
2
|
|
|
2
|
1
|
1415
|
my ($self) = @_; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
6
|
my $message = ''; |
88
|
2
|
|
|
|
|
95
|
foreach my $block ($self->list_blocks) { |
89
|
3
|
|
|
|
|
13
|
$message .= $block->stringify; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
8
|
return $message; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub AUTOLOAD { |
96
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
97
|
0
|
|
|
|
|
|
$self->overload; |
98
|
0
|
|
|
|
|
|
return $MooseX::App::Null::NULL; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
{ |
102
|
|
|
|
|
|
|
package MooseX::App::Null; |
103
|
|
|
|
|
|
|
|
104
|
15
|
|
|
15
|
|
9944
|
use strict; |
|
15
|
|
|
|
|
41
|
|
|
15
|
|
|
|
|
542
|
|
105
|
15
|
|
|
15
|
|
109
|
use warnings; |
|
15
|
|
|
|
|
34
|
|
|
15
|
|
|
|
|
1402
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
use overload |
108
|
0
|
|
|
0
|
|
0
|
'bool' => sub { 0 }, |
109
|
0
|
|
|
0
|
|
0
|
'""' => sub { '' }, |
110
|
15
|
|
|
15
|
|
118
|
'0+' => sub { 0 }; |
|
15
|
|
|
0
|
|
49
|
|
|
15
|
|
|
|
|
190
|
|
|
0
|
|
|
|
|
0
|
|
111
|
|
|
|
|
|
|
our $NULL = bless {}, __PACKAGE__; |
112
|
0
|
|
|
0
|
|
|
sub AUTOLOAD { return $NULL } |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
116
|
|
|
|
|
|
|
1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=encoding utf8 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 NAME |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
MooseX::App::Message::Envelope - Message presented to the user |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 DESCRIPTION |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Whenever MooseX::App needs to pass a message to the user, it does so by |
129
|
|
|
|
|
|
|
generating a MooseX::App::Message::Envelope object. The object usually |
130
|
|
|
|
|
|
|
contains one or more blocks (L<MooseX::App::Message::Block>) and can be |
131
|
|
|
|
|
|
|
easily stringified. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Usually a MooseX::App::Message::Envelope object is generated and returned |
134
|
|
|
|
|
|
|
by the L<new_with_command method in MooseX::App::Base|MooseX::App::Base/new_with_command> |
135
|
|
|
|
|
|
|
if there is an error or if the user requests help. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
To avoid useless object type checks when working with this method, |
138
|
|
|
|
|
|
|
MooseX::App::Message::Envelope follows the Null-class pattern. So you can do |
139
|
|
|
|
|
|
|
this, no matter if new_with_command fails or not: |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
MyApp->new_with_command->some_method->only_called_if_successful; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
If |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 METHODS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 stringify |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Stringifies the messages |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 overload |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This method is called whenever the object is stringified via overload. In this |
154
|
|
|
|
|
|
|
case it prints the message on either STDERR or STDOUT, and exits the process |
155
|
|
|
|
|
|
|
with the given exitcode (if any). |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 add_block |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Adds a new message block. Param must be a L<MooseX::App::Message::Block> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 list_blocks |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Returns a list on message blocks. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 blocks |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Message block accessor. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 exitcode |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Exitcode accessor. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 has_exitcode |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Check if exitcode is set. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 OVERLOAD |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Stringification of this object is overloaded. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 AUTOLOAD |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
You can call any method on the message class. |