File Coverage

blib/lib/Junction/Quotelike.pm
Criterion Covered Total %
statement 31 40 77.5
branch 3 6 50.0
condition 1 3 33.3
subroutine 6 10 60.0
pod n/a
total 41 59 69.4


line stmt bran cond sub pod time code
1             package Junction::Quotelike;
2            
3             =head1 NAME
4            
5             Junction::Quotelike - quotelike junction operators
6            
7             =cut
8            
9 1     1   23219 use strict;
  1         3  
  1         36  
10 1     1   6 use warnings;
  1         2  
  1         31  
11            
12 1     1   5 use Carp qw/croak/;
  1         6  
  1         60  
13            
14 1     1   831 use PerlX::QuoteOperator qw//;
  1         138213  
  1         26  
15 1     1   1130 use Perl6::Junction qw/all any none one/;
  1         12374  
  1         561  
16            
17            
18             =head1 VERSION
19            
20             This document describes version 0.01 of Junction::Quotelike,
21             released Sun Feb 14 16:20:27 CET 2010 @680 /Internet Time/
22            
23             =cut
24            
25             our $VERSION = 0.01;
26            
27             =head1 SYNOPSIS
28            
29             use Junction::Quotelike qw/qany/;
30            
31             my $x = 'foo';
32            
33             print "is foo!" if $x eq qany/foo bar baz/; #is foo
34            
35            
36             =head1 DESCRIPTION
37            
38             Junction::Quotelike glues Perl6::Junction and PerlX::QuoteOperator together to
39             provide quotelike junction operators.
40            
41             =head2 Operators
42            
43             Junction::Quotelike defines the following Operators
44            
45             =cut
46            
47             =head3 qany//
48            
49             Quotelike version of any(). Returns a junction that tests against one more of
50             its Elements. See L<> for details
51            
52             =head3 qall//
53            
54             Quotelike version of all(). Returns a junction that tests against all of its
55             Elements. See L<> for details
56            
57             =head3 qone//
58            
59             Quotelike version of one(). Returns a junction that tests against one (and only
60             one) of its Elements. See L<> for details
61            
62             =head3 qnone//
63            
64             Quotelike version of none(). Returns a junction that tests against none of its
65             Elements. See L<> for details
66            
67             =cut
68            
69            
70             sub import
71             {
72 1     1   11 my $class = shift;
73 1         3 my $names;
74             my $caller;
75 0         0 my $valid;
76 0         0 my $ctx;
77 0         0 my %code;
78            
79 1         2 $caller = caller;
80            
81 1         4 $valid = any(qw/qany qall qone qnone/);
82 1         33 $ctx = PerlX::QuoteOperator->new;
83            
84             %code =
85             (
86 0     0   0 qany => sub (@){ any(@_)},
87 0     0   0 qall => sub (@){ all(@_)},
88 0     0   0 qone => sub (@){ one(@_)},
89 0     0   0 qnone => sub (@){ none(@_)},
90 1         16 );
91            
92 1 50 33     15 if (@_ == 1 && ref $_[0])
    50          
93             {
94 0         0 $names = shift;
95             }
96             elsif(@_ > 0)
97             {
98 1         2 $names = {};
99 1         3 foreach my $name (@_)
100             {
101 1         5 $names->{$name} = $name;
102             }
103             }
104             else
105             {
106 0         0 croak "no import spec";
107             }
108            
109 1         3 foreach my $name (keys %{$names})
  1         3  
110             {
111 1 50       87 croak "bad import spec: $name" unless $name eq $valid;
112             }
113            
114 1         59 foreach my $name (keys %{$names})
  1         3  
115             {
116 1         11 $ctx->import($names->{$name},
117             {-emulate => 'qw', -with => $code{$name}, -parser => 1},
118             $caller );
119             }
120             }
121            
122             =head2 Export
123            
124             Junction::Quotelike exports qany qall qnone qone upon request. You can import
125             one or more of them in the usual way.
126            
127             use Junction::Quotelike qw'qall';
128            
129             or
130            
131             use Junction::Quotelike qw'qany qall';
132            
133             Altnernativly you can rename them while importing:
134            
135             use Junction::Quotelike { qany => 'any', qall => 'all' };
136            
137             This would export the operators qany and qall to your namespace renamed to any
138             and all, so you can write:
139            
140             my $anyjunction = any /foo bar baz/;
141             my $alljunction = all /foo bar baz/;
142            
143             You must however import at least one operator into your namespace.
144            
145            
146             =head1 DIAGNOSTICS
147            
148             =over
149            
150             =item "bad import spec: %s"
151            
152             You requested an invalid operator to be exported. Currently valid operators are:
153             qany|qall|qone|qnone.
154            
155            
156             =item "no import spec"
157            
158             You didn't request any operator to be exported. Without exports this module is
159             useless.
160            
161            
162             =back
163            
164             =head1 BUGS
165            
166             There are undoubtedly serious bugs lurking somewhere.
167             If you believe you have found a new, undocumented or ill documented bug,
168             then please drop me a mail to blade@dropfknuck.net .
169            
170             =over
171            
172             =item Delimiters
173            
174             The list of supported delimiters is a bit more restricted than with standard
175             quotelike operators. Currently tested and supported are:
176            
177             '/', '\', '!'
178            
179             On the other hand known I<> to work are
180            
181             ''', '#'. '()', '[]', '{}'
182            
183             In general, all bracketing delimiters are known not to work, and other non
184             bracketing delimiters may work or not, but aren't tested (yet). These are
185             restrictions from PerlX::QuoteOperator. With all these limitations this module
186             may better be called Junction::Quotelikelike.
187            
188             =back
189            
190             =head1 CAVEATS
191            
192             Junction::Quotelike relies on the dark magic performed by PerlX::QuoteOperator
193             which enables custom quotelike operators. While this seems to work very stable,
194             you should be aware that there may be some unexpected side effects. See
195             PerlX::QuoteOperator for details.
196            
197             It is not possible to use the operators directly witout importing them.
198             Qualifying them like Junction::Quotelike::qany/foo bar/ B<>.
199             I don't think that's bug since using qualified names would make the use of this
200             module rather pointless.
201            
202            
203             =head1 SEE ALSO
204            
205             Junction::Quotelike doesn't really do much on itself but rather relies on the
206             services of these Modules to perform its job.
207            
208             =over
209            
210             =item L<>
211            
212             Perl6::Junction defines the semantics for junctions used by this module. If
213             you're intrested in junctions without quotelike behavior this your friend.
214            
215             =item L<>
216            
217             PerlX::QuoteOperator enables the definition of custom quotelike operators in a
218             straightforward manner.
219            
220             =back
221            
222             =head1 WHY?
223            
224             Why not?
225            
226             As of this writing i am working on some slightly complex piece of code that
227             makes heavy use of junctions (as provided by Perl6::Junction). While this makes
228             my code way less complex i'm still forced to write a lot lines like
229            
230             ...
231             $valid = any(qw/this that something else/);
232             ...
233            
234             Sure that's not that bad, but it doesn't look nice to me. Writing it like:
235            
236             ...
237             $valid = qany /this that something else/;
238             ...
239            
240             Looks a lot better to me.
241            
242             =head1 AUTHOR
243            
244             blackhat.blade (formerly Lionel Mehl)
245             dropfknuck.net
246            
247            
248             =head1 COPYRIGHT
249            
250             Copyright (c) 2010 blackhat.blade, dropfknuck.net
251             This module is free software. It may be used, redistributed
252             and/or modified under the terms of the Artistic license.
253            
254             =cut
255            
256             1;
257            
258             __END__