line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Pulp; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
522781
|
use warnings; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
196
|
|
4
|
6
|
|
|
6
|
|
33
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
350
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Data::Pulp - Pulp your data into a consistent goop |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.01 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Data::Pulp; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $pulper = pulper |
23
|
|
|
|
|
|
|
case { $_ eq 'apple' } then { 'APPLE' } |
24
|
|
|
|
|
|
|
case { $_ eq 'banana' } |
25
|
|
|
|
|
|
|
case { $_ eq 'cherry' } then { 'CHERRY' } |
26
|
|
|
|
|
|
|
case { ref eq 'SCALAR' } then { 'SCALAR' } |
27
|
|
|
|
|
|
|
empty { 'empty' } |
28
|
|
|
|
|
|
|
nil { 'nil' } |
29
|
|
|
|
|
|
|
case { m/xyzzy/ } then { 'Nothing happens.' } |
30
|
|
|
|
|
|
|
default { croak "Don't understand $_" } |
31
|
|
|
|
|
|
|
; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$pulper->pulp( 'apple' ) # APPLE |
34
|
|
|
|
|
|
|
$pulper->pulp( 'banana' ) # CHERRY |
35
|
|
|
|
|
|
|
$pulper->pulp( 'xyyxyzzyx' ) # Nothing happens. |
36
|
|
|
|
|
|
|
$pulper->pulp( undef ) # nil |
37
|
|
|
|
|
|
|
$pulper->pulp( '' ) # empty |
38
|
|
|
|
|
|
|
$pulper->pulp( '1' ) # Throw an exception: Don't understand 1 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# You can also operate on an array or hash |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $set = $pulper->prepare( [ qw/apple banana cherry/, '', undef, qw/xyzzy xyyxyzzyx grape/ ] ) |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$set->all # APPLE, CHERRY, CHERRY, empty, nil, Nothing happens ... |
45
|
|
|
|
|
|
|
$set->first # APPLE |
46
|
|
|
|
|
|
|
$set->last # Throw an exception: Don't understand grape |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 DESCRIPTION |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Data::Pulp is a tool for coercing and/or validating input data. Instead of doing this: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if ( defined $value ) { |
53
|
|
|
|
|
|
|
if ( ref $value eq ... ) { |
54
|
|
|
|
|
|
|
... |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ( $value =~ m/.../ ) { |
57
|
|
|
|
|
|
|
... |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
... |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
You can do something like this: |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $pulper = pulper |
67
|
|
|
|
|
|
|
case { $_ eq ... } then { ... } |
68
|
|
|
|
|
|
|
case { m/.../ } then { ... } |
69
|
|
|
|
|
|
|
nil { ... # Do action if value is undefined } |
70
|
|
|
|
|
|
|
; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$pulper->pulp( $value ) |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
A pulper can act transparently on a single value, ARRAY, or HASH: |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $set = $pulper->prepare( $value ) # A single value list |
77
|
|
|
|
|
|
|
my $set = $pulper->prepare( [ $value, ... ] ) |
78
|
|
|
|
|
|
|
my $set = $pulper->prepare( { key => $value, ... } ) # Throws away the keys, basically |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
So, given a subroutine: |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub method { |
83
|
|
|
|
|
|
|
my $data = shift; |
84
|
|
|
|
|
|
|
# $data could be a single value, or an array, or even a hash |
85
|
|
|
|
|
|
|
my $set = $pulper->prepare( $data ) |
86
|
|
|
|
|
|
|
my @data = $set->all # Get all the data coerced how you want |
87
|
|
|
|
|
|
|
# or throw an exception if something bad is encountered |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
... |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
6
|
|
|
6
|
|
7347
|
use Moose(); |
|
6
|
|
|
|
|
3398439
|
|
|
6
|
|
|
|
|
202
|
|
95
|
6
|
|
|
6
|
|
11413
|
use Data::Pulp::Carp; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
41
|
|
96
|
|
|
|
|
|
|
|
97
|
12
|
|
|
12
|
|
157
|
sub EXPORT () {qw/ |
98
|
|
|
|
|
|
|
case if_value if_type if_object |
99
|
|
|
|
|
|
|
then empty nil default |
100
|
|
|
|
|
|
|
pulper |
101
|
|
|
|
|
|
|
/} |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
23
|
use Sub::Exporter -setup => { |
104
|
|
|
|
|
|
|
exports => [ |
105
|
|
|
|
|
|
|
EXPORT, |
106
|
|
|
|
|
|
|
], |
107
|
|
|
|
|
|
|
groups => { |
108
|
|
|
|
|
|
|
default => [ EXPORT ], |
109
|
|
|
|
|
|
|
}, |
110
|
6
|
|
|
6
|
|
1153
|
}; |
|
6
|
|
|
|
|
13
|
|
111
|
|
|
|
|
|
|
|
112
|
6
|
|
|
6
|
|
7625
|
use Data::Pulp::Pulper; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub case (&@) { return case => @_ } |
115
|
|
|
|
|
|
|
sub if_value (&@) { return if_value => @_ } |
116
|
|
|
|
|
|
|
sub if_type (&@) { return if_type => @_ } |
117
|
|
|
|
|
|
|
sub if_object (&@) { return if_object => @_ } |
118
|
|
|
|
|
|
|
sub then (&@) { return then => @_ } |
119
|
|
|
|
|
|
|
sub empty (&@) { return empty => @_ } |
120
|
|
|
|
|
|
|
sub nil (&@) { return nil => @_ } |
121
|
|
|
|
|
|
|
sub default (&@) { return default => @_ } |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub pulper { |
124
|
|
|
|
|
|
|
shift if $_[0] eq __PACKAGE__; |
125
|
|
|
|
|
|
|
return Data::Pulp::Pulper->parse( @_ ); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 AUTHOR |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Robert Krimen, C<< <rkrimen at cpan.org> >> |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 BUGS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-data-pulp at rt.cpan.org>, or through |
135
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Pulp>. I will be notified, and then you'll |
136
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SUPPORT |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
perldoc Data::Pulp |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
You can also look for information at: |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=over 4 |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Pulp> |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Data-Pulp> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * CPAN Ratings |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Data-Pulp> |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * Search CPAN |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Data-Pulp/> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=back |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Copyright 2009 Robert Krimen, all rights reserved. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
179
|
|
|
|
|
|
|
under the same terms as Perl itself. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; # End of Data::Pulp |