File Coverage

blib/lib/Syntax/Operator/Zip.pm
Criterion Covered Total %
statement 35 42 83.3
branch 9 12 75.0
condition n/a
subroutine 9 12 75.0
pod 1 4 25.0
total 54 70 77.1


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2021-2023 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Zip 0.06;
7              
8 4     4   707039 use v5.14;
  4         38  
9 4     4   22 use warnings;
  4         8  
  4         98  
10              
11 4     4   19 use Carp;
  4         8  
  4         1422  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - infix operator to compose two lists together
19              
20             =head1 SYNOPSIS
21              
22             On Perl v5.38 or later:
23              
24             use Syntax::Operator::Zip;
25              
26             foreach (@xvals Z @yvals) {
27             my ($x, $y) = @$_;
28             say "Value $x is associated with value $y";
29             }
30              
31             Or on Perl v5.14 or later:
32              
33             use v5.14;
34             use Syntax::Operator::Zip qw( zip );
35              
36             foreach (zip \@xvals, \@yvals) {
37             my ($x, $y) = @$_;
38             say "Value $x is associated with value $y";
39             }
40              
41             =head1 DESCRIPTION
42              
43             This module provides infix operators that compose two lists of elements by
44             associating successive elements from the left and right-hand lists together,
45             forming a new list.
46              
47             Support for custom infix operators was added in the Perl 5.37.x development
48             cycle and is available from development release v5.37.7 onwards, and therefore
49             in Perl v5.38 onwards. The documentation of L
50             describes the situation in more detail.
51              
52             While Perl versions before this do not support custom infix operators, they
53             can still be used via C and hence L.
54             Custom keywords which attempt to parse operator syntax may be able to use
55             these.
56              
57             Additionally, earlier versions of perl can still use the function-like
58             wrapper versions of these operators. Even though the syntax appears like a
59             regular function call, the code is compiled internally into the same more
60             efficient operator internally, so will run without the function-call overhead
61             of a regular function.
62              
63             =cut
64              
65             sub import
66             {
67 3     3   26 my $pkg = shift;
68 3         11 my $caller = caller;
69              
70 3         11 $pkg->import_into( $caller, @_ );
71             }
72              
73             sub unimport
74             {
75 0     0   0 my $pkg = shift;
76 0         0 my $caller = caller;
77              
78 0         0 $pkg->unimport_into( $caller, @_ );
79             }
80              
81 3     3 0 10 sub import_into { shift->apply( 1, @_ ) }
82 0     0 0 0 sub unimport_into { shift->apply( 0, @_ ) }
83              
84             sub apply
85             {
86 3     3 0 5 my $pkg = shift;
87 3         9 my ( $on, $caller, @syms ) = @_;
88              
89 3 100       14 @syms or @syms = qw( Z M );
90              
91 3         9 my %syms = map { $_ => 1 } @syms;
  6         19  
92 3         8 foreach (qw( Z M )) {
93 6 100       28 next unless delete $syms{$_};
94              
95             $on ? $^H{"Syntax::Operator::Zip/$_"}++
96 4 50       27 : delete $^H{"Syntax::Operator::Zip/$_"};
97             }
98              
99 3         8 foreach (qw( zip mesh )) {
100 6 100       19 next unless delete $syms{$_};
101              
102 4     4   35 no strict 'refs';
  4         10  
  4         630  
103 2 50       6 $on ? *{"${caller}::$_"} = \&{$_}
  2         9  
  2         5  
104             : warn "TODO: implement unimport of package symbol";
105             }
106              
107 3 50       2026 croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms;
  0            
108             }
109              
110             =head1 OPERATORS
111              
112             =head2 Z
113              
114             my @result = @lhs Z @rhs;
115              
116             # returns [$lhs[0], $rhs[0]], [$lhs[1], $rhs[1]], ...
117              
118             Yields a list of array references, each containing a pair of items from the
119             two operand lists. If one of the operand lists is shorter than the other, the
120             missing elements will be filled in with C so that every array reference
121             in the result contains exactly two items.
122              
123             =head2 M
124              
125             my @result = @lhs M @rhs;
126              
127             # returns $lhs[0], $rhs[0], $lhs[1], $rhs[1], ...
128              
129             Yields a list of the values from its operand lists, rearranged into pairs and
130             flattened. If one of the operand lists is shorter than the other, the missing
131             elements will be filled in with C so that the result is correctly lined
132             up.
133              
134             The result of this operator is useful for constructing hashes from two lists
135             containing keys and values
136              
137             my %hash = @keys M @values;
138              
139             =cut
140              
141             =head1 FUNCTIONS
142              
143             As a convenience, the following functions may be imported which implement the
144             same behaviour as the infix operators, though are accessed via regular
145             function call syntax. The two lists for these functions to operate on must be
146             passed as references to arrays (either named variables, or anonymously
147             constructed by C<[...]>).
148              
149             =head2 zip
150              
151             my @result = zip( \@lhs, \@rhs );
152              
153             A function version of the L operator.
154              
155             See also L.
156              
157             =head2 mesh
158              
159             my @result = mesh( \@lhs, \@rhs );
160              
161             A function version of the L operator.
162              
163             See also L.
164              
165             =cut
166              
167             =head1 AUTHOR
168              
169             Paul Evans
170              
171             =cut
172              
173             0x55AA;