File Coverage

blib/lib/Switch/Again.pm
Criterion Covered Total %
statement 39 39 100.0
branch 16 16 100.0
condition 11 11 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 78 78 100.0


line stmt bran cond sub pod time code
1             package Switch::Again;
2 7     7   817721 use 5.006; use strict; use warnings; our $VERSION = '1.02';
  7     7   32  
  7     7   43  
  7         47  
  7         279  
  7         39  
  7         23  
  7         642  
3 7     7   3702 use Struct::Match qw/struct/;
  7         182088  
  7         72  
4 7     7   1194 use base qw/Import::Export/;
  7         17  
  7         1529  
5              
6             our %EX = (
7             'switch' => [qw/all/],
8             'sr' => [qw/all/]
9             );
10              
11             BEGIN {
12             @STRUCT{qw/Regexp CODE/} = (
13 12         87 sub { $_[1] =~ $_[0]; },
14 12         22 sub { my $v = eval { $_[0]->($_[1]) }; }
  12         37  
15 7     7   3365 );
16             }
17              
18             sub switch {
19 19     19 1 985595 my ($value, $default, @cases);
20 19 100       101 $value = shift if (scalar @_ % 2);
21              
22             $_[0] eq 'default'
23 13         116 ? do { shift; $default = shift }
  13         38  
24 19 100       159 : do { push @cases, { ref => $STRUCT{REF}($_[0]), case => shift, cb => shift } }
  39         326  
25             while (@_); # I could map to a hash but...
26              
27             my $evil = sub {
28 39     39   25659 my ($val, @result) = ($_[0]);
29             eval {
30 79         288 @result = ($STRUCT{$_->{ref}}($_->{case}, $val));
31 79 100 100     3006 @result = () if @result && $result[0] eq '';
32 79         275 @result;
33             } and do {
34             @result = ref $_->{cb} eq 'CODE' ? $_->{cb}->($val, @result) : $_->{cb}
35 39 100 100     111 } and last for @cases;
  28   100     164  
36 38 100 100     413 @result ? wantarray ? @result : shift @result : $default && $default->($val);
    100          
37 19         203 };
38              
39 19 100       96 defined $value ? $evil->($value) : $evil;
40             }
41              
42             sub sr {
43 5     5 1 227230 my ($search, $replace) = @_;
44 5 100   5   111 return sub {my $v589 = shift; return '' unless $v589 =~ m/$search/; $v589 =~ s/$search/$replace/g; $v589;};
  5         10  
  5         70  
  2         35  
  2         18  
45             }
46              
47             1;
48              
49             __END__;
50              
51             =head1 NAME
52              
53             Switch::Again - Switch`ing
54              
55             =head1 VERSION
56              
57             Version 1.02
58              
59             =cut
60              
61             =head1 SYNOPSIS
62              
63             use Switch::Again qw/switch/;
64             my $switch = switch
65             'a' => sub {
66             return 1;
67             },
68             'b' => sub {
69             return 2;
70             },
71             'c' => sub {
72             return 3;
73             },
74             'default' => sub {
75             return 4;
76             }
77             ;
78             my $val = $switch->('a'); # 1
79              
80             ...
81              
82             use Switch::Again qw/all/;
83             my $val = switch 'e',
84             sr('(search)', 'replace') => sub {
85             return 1;
86             },
87             qr/(a|b|c|d|e)/ => sub {
88             return 2;
89             },
90             sub { $_[0] == 1 } => sub {
91             return 3;
92             },
93             'default' => sub {
94             return 4;
95             }
96             ; # 2
97              
98             =cut
99              
100             =head1 EXPORT
101              
102             =head2 switch
103              
104             Keyword for either generating a reusable switch statement or calling the generated switch function directly, it accepts key/value pairs where the key is to be evaluated against any passed variable and the value is what is returned when the key matches. To generate a reusable switch pass an even number of params, to evaluate a single switch statement pass an odd number of params where the first param is the value that will be evaluated. Optionally a default key can be set which will be called if no other 'key' matches are met. keys can be any type of data struct as internally B<Struct::Match> is used.
105              
106             Reusable:
107              
108             my $switch = switch
109             string => sub { ... },
110             qr/match this regex/ => sub { ... },
111             sr('(search)', 'replace') => sub { ... },
112             sub { $_[0] eq 'evaluate' } => sub { ... },
113             { a => 'b', c => 'd' } => sub { ... },
114             [ 'e', 'f' ] => sub { ... },
115             default => { 'the fallback' }
116              
117             Single:
118              
119             my $switch = switch 'find search'
120             string => sub { ... },
121             qr/match this regex/ => sub { ... },
122             sr('(search)', 'replace') => sub { ... },
123             sub { $_[0] eq 'evaluate' } => sub { ... },
124             { a => 'b', c => 'd' } => sub { ... },
125             [ 'e', 'f' ] => sub { ... },
126             default => { 'the fallback' }
127              
128             =cut
129              
130             =head2 sr
131              
132             A helper function to be used in conjunction with the switch statement, it allows you to search and replace the value being evaluated by the switch statement, if the search and replace is successfull then that keys value will be executed with the updated string.
133              
134             sr('(search)', 'replace');
135              
136             =cut
137              
138             =head1 AUTHOR
139              
140             LNATION, C<< <thisusedtobeanemail at gmail.com> >>
141              
142             =head1 BUGS
143              
144             Please report any bugs or feature requests to C<bug-switch-again at rt.cpan.org>, or through
145             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Switch-Again>. I will be notified, and then you'll
146             automatically be notified of progress on your bug as I make changes.
147              
148             =head1 SUPPORT
149              
150             You can find documentation for this module with the perldoc command.
151              
152             perldoc Switch::Again
153              
154             You can also look for information at:
155              
156             =over 4
157              
158             =item * RT: CPAN's request tracker (report bugs here)
159              
160             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Switch-Again>
161              
162             =item * AnnoCPAN: Annotated CPAN documentation
163              
164             L<http://annocpan.org/dist/Switch-Again>
165              
166             =item * CPAN Ratings
167              
168             L<http://cpanratings.perl.org/d/Switch-Again>
169              
170             =item * Search CPAN
171              
172             L<http://search.cpan.org/dist/Switch-Again/>
173              
174             =back
175              
176             =head1 ACKNOWLEDGEMENTS
177              
178             =head1 LICENSE AND COPYRIGHT
179              
180             Copyright 2018 Robert Acock.
181              
182             This program is free software; you can redistribute it and/or modify it
183             under the terms of the the Artistic License (2.0). You may obtain a
184             copy of the full license at:
185              
186             L<http://www.perlfoundation.org/artistic_license_2_0>
187              
188             Any use, modification, and distribution of the Standard or Modified
189             Versions is governed by this Artistic License. By using, modifying or
190             distributing the Package, you accept this license. Do not use, modify,
191             or distribute the Package, if you do not accept this license.
192              
193             If your Modified Version has been derived from a Modified Version made
194             by someone other than you, you are nevertheless required to ensure that
195             your Modified Version complies with the requirements of this license.
196              
197             This license does not grant you the right to use any trademark, service
198             mark, tradename, or logo of the Copyright Holder.
199              
200             This license includes the non-exclusive, worldwide, free-of-charge
201             patent license to make, have made, use, offer to sell, sell, import and
202             otherwise transfer the Package with respect to any patent claims
203             licensable by the Copyright Holder that are necessarily infringed by the
204             Package. If you institute patent litigation (including a cross-claim or
205             counterclaim) against any party alleging that the Package constitutes
206             direct or contributory patent infringement, then this Artistic License
207             to you shall terminate on the date that such litigation is filed.
208              
209             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
210             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
211             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
212             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
213             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
214             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
215             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
216             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217              
218              
219             =cut