Pulp.pm

filter: Statementstmt 100% 31 / 31 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 10 totaltotal 81.3% 48 / 59 CC 2 SCAR 1.2
CC
2
Cov
97%
pulper
2.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Pulp/Pulper.pm ↑ Summary Pulp/Carp.pm »
1 package Data::Pulp;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 6 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 ▶use Moose();
Subroutine BEGIN: called CC=1 SCAR=0.0
95 6 ▶use Data::Pulp::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 12 ▶sub EXPORT () {qw/
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
98 case if_value if_type if_object
99 then empty nil default
100 pulper
101 /}
102  
103 6 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
104 exports => [
105 EXPORT,
106 ],
107 groups => {
108 default => [ EXPORT ],
109 },
110 6 };
111  
112 6 ▶use Data::Pulp::Pulper;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 9 ▶sub case (&@) { return case => @_ }
Subroutine case: called CC=1 SCAR=0.0
Pod undocumented
115 1 ▶sub if_value (&@) { return if_value => @_ }
Subroutine if_value: called CC=1 SCAR=0.0
Pod undocumented
116 1 ▶sub if_type (&@) { return if_type => @_ }
Subroutine if_type: called CC=1 SCAR=0.0
Pod undocumented
117 1 ▶sub if_object (&@) { return if_object => @_ }
Subroutine if_object: called CC=1 SCAR=0.0
Pod undocumented
118 10 ▶sub then (&@) { return then => @_ }
Subroutine then: called CC=1 SCAR=0.0
Pod undocumented
119 2 ▶sub empty (&@) { return empty => @_ }
Subroutine empty: called CC=1 SCAR=0.0
Pod undocumented
120 2 ▶sub nil (&@) { return nil => @_ }
Subroutine nil: called CC=1 SCAR=0.0
Pod undocumented
121 2 ▶sub default (&@) { return default => @_ }
Subroutine default: called CC=1 SCAR=0.0
Pod undocumented
122  
123 3 ▶sub pulper {
Subroutine pulper: called CC=2 SCAR=7.2
Pod undocumented
124 3 ▶ shift if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 0 3 3
125 3 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 6 1; # End of Data::Pulp
« Pulp/Pulper.pm ↑ Summary Pulp/Carp.pm »