1 package Perl6::Junction;
4 use Perl6::Junction::All;
5 use Perl6::Junction::Any;
6 use Perl6::Junction::None;
7 use Perl6::Junction::One;
10 our $VERSION = '1.40000';
12 our @ISA = qw/ Exporter /;
13 my @routines = qw/ all any none one /;
14 our @EXPORT_OK = @routines;
15 our %EXPORT_TAGS = ( ALL => [@routines] );
18 return Perl6::Junction::All->new(@_);
22 return Perl6::Junction::Any->new(@_);
26 return Perl6::Junction::None->new(@_);
30 return Perl6::Junction::One->new(@_);
39 Perl6::Junction - Perl6 style Junction operators in Perl5.
43 use Perl6::Junction qw/ all any none one /;
45 if (any(@grant) eq 'su') {
49 if (all($foo, $bar) >= 10) {
53 if (qr/^\d+$/ == all(@answers)) {
57 if (all(@input) <= @limits) {
61 if (none(@pass) eq 'password') {
65 if (one(@answer) == 42) {
71 This is a lightweight module which provides 'Junction' operators, the most
72 commonly used being C<any> and C<all>.
74 Inspired by the Perl6 design docs,
75 L<http://dev.perl.org/perl6/doc/design/exe/E06.html>.
77 Provides a limited subset of the functionality of L<Quantum::Superpositions>,
78 see L</"SEE ALSO"> for comment.
80 Notice in the L</SYNOPSIS> above, that if you want to match against a
81 regular expression, you must use C<==> or C<!=>. B<Not> C<=~> or C<!~>. You
82 must also use a regex object, such as C<qr/\d/>, not a plain regex such as
90 Returns an object which overloads the following operators:
92 '<', '<=', '>', '>=', '==', '!=',
93 'lt', 'le', 'gt', 'ge', 'eq', 'ne',
95 Returns true only if B<all> arguments test true according to the operator
100 Returns an object which overloads the following operators:
102 '<', '<=', '>', '>=', '==', '!=',
103 'lt', 'le', 'gt', 'ge', 'eq', 'ne',
105 Returns true if B<any> argument tests true according to the operator used.
109 Returns an object which overloads the following operators:
111 '<', '<=', '>', '>=', '==', '!=',
112 'lt', 'le', 'gt', 'ge', 'eq', 'ne',
114 Returns true only if B<no> argument tests true according to the operator
119 Returns an object which overloads the following operators:
121 '<', '<=', '>', '>=', '==', '!=',
122 'lt', 'le', 'gt', 'ge', 'eq', 'ne',
124 Returns true only if B<one and only one> argument tests true according to
127 =head1 ALTERING JUNCTIONS
129 You cannot alter junctions. Instead, you can create new junctions out of old
130 junctions. You can do this by calling the C<values> method on a junction.
132 my $numbers = any(qw/1 2 3 4 5/);
133 print $numbers == 3 ? 'Yes' : 'No'; # Yes
135 $numbers = any( grep { $_ != 3 } $numbers->values );
136 print $numbers == 3 ? 'Yes' : 'No'; # No
140 'all', 'any', 'none', 'one', as requested.
142 All subroutines can be called by its fully qualified name, if you don't
147 if (Perl6::Junction::any( @questions )) {
153 When comparing against a regular expression, you must remember to use a
154 regular expression object: C<qr/\d/> B<Not> C</d/>. You must also use either
155 C<==> or C<!=>. This is because C<=~> and C<!~> cannot be overriden.
159 Add overloading for arithmetic operators, such that this works:
161 $result = any(2,3,4) * 2;
163 if ($result == 8) {...}
165 =head1 SUPPORT / BUGS
167 Submit to the CPAN bugtracker L<http://rt.cpan.org>
171 L<Quantum::Superpositions> provides the same functionality as this, and
172 more. However, this module provides this limited functionality at a much
173 greater runtime speed, with my benchmarks showing between 500% and 6000%
176 L<http://dev.perl.org/perl6/doc/design/exe/E06.html> - "The Wonderful World
183 =head1 ACKNOWLEDGEMENTS
185 Thanks to C<Curtis "Ovid" Poe> for the L</"ALTERING JUNCTIONS"> changes in
188 =head1 COPYRIGHT AND LICENSE
190 Copyright 2005, Carl Franks. All rights reserved.
192 This library is free software; you can redistribute it and/or modify it under
193 the same terms as Perl itself (L<perlgpl>, L<perlartistic>).