blib/lib/Syntax/Operator/ExistsOr.pm

filter: Statementstmt 45.8% 11 / 24 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 3 / 8 Podpod 0.0% 0 / 3 totaltotal 35.9% 14 / 39 CC 3 SCAR 15.5
CC
3
Cov
39%
apply
12.0
import
2.0
unimport
2.0
CRAP 4.7
↑ Summary hax/OP_HELEMEXISTSOR.c.inc »
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, 2022-2023 -- leonerd@leonerd.org.uk
5  
6 package Syntax::Operator::ExistsOr 0.03;
7  
8 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 require XSLoader;
14 1 XSLoader::load( __PACKAGE__, our $VERSION );
15  
16 =head1 NAME
17  
18 C<Syntax::Operator::ExistsOr> - an infix operator sensitive to hash element existence
19  
20 =head1 SYNOPSIS
21  
22 On a suitable perl version:
23  
24 use Syntax::Operator::ExistsOr;
25  
26 sub func ( %args ) {
27 my $count = $args{count} \\ 10;
28  
29 say "Count is ", $count // "<undef>";
30 }
31  
32 func( count => 20 );
33 func();
34  
35 func( count => undef );
36  
37 =head1 DESCRIPTION
38  
39 This module provides an infix operator that similar to the defined-or C<//>
40 core perl operator, but which cares about hash element existence rather than
41 definedness.
42  
43 Support for custom infix operators was added in the Perl 5.37.x development
44 cycle and is available from development release v5.37.7 onwards, and therefore
45 in Perl v5.38 onwards. The documentation of L<XS::Parse::Infix>
46 describes the situation in more detail.
47  
48 While Perl versions before this do not support custom infix operators, they
49 can still be used via C<XS::Parse::Infix> and hence L<XS::Parse::Keyword>.
50 Custom keywords which attempt to parse operator syntax may be able to use
51 these.
52  
53 This module does not provide wrapper functions for the operators, as their
54 inherent short-circuiting behaviour would appear confusing when expressed in
55 function-like syntax.
56  
57 =cut
58  
59 sub import
60 0 ▶{
Subroutine import: not called CC=1 SCAR=6.9
61 0 my $pkg = shift;
62 0 my $caller = caller;
63  
64 0 $pkg->import_into( $caller, @_ );
65 }
66  
67 sub unimport
68 0 ▶{
Subroutine unimport: not called CC=1 SCAR=6.9
69 0 my $pkg = shift;
70 0 my $caller = caller;
71  
72 0 $pkg->unimport_into( $caller, @_ );
73 }
74  
75 0 ▶sub import_into { shift->apply( 1, @_ ) }
Subroutine import_into: not called CC=1 SCAR=6.9
Pod undocumented
76 0 ▶sub unimport_into { shift->apply( 0, @_ ) }
Subroutine unimport_into: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 sub apply
79 0 ▶{
Subroutine apply: not called CC=3 SCAR=24.8
Pod undocumented
80 0 my $pkg = shift;
81 0 my ( $on, $caller, @syms ) = @_;
82  
83 0 ▶ @syms or @syms = qw( existsor \\\\ );
Branch: 1 branch
BranchTrueFalseTotal
@syms or @syms = ("existsor", "\\\\") 0 0 0
84  
85 0 $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( existsor \\\\ ) );
86  
87 0 ▶ croak "Unrecognised import symbols @syms" if @syms;
Branch: 1 branch
BranchTrueFalseTotal
if @syms 0 0 0
88 }
89  
90 =head1 OPERATORS
91  
92 =head2 \\
93  
94 my $value = $hash{$key} \\ EXPR;
95  
96 The lefthand operand must be a hash element access (i.e. C<$hash{$key}> or
97 C<< $href->{$key} >> for some expressions yielding a key and a hashref).
98  
99 If the hash contains the given key then the operator yields its value (even if
100 that value is C<undef>). If the key does not exist in the hash, then the
101 righthand operand will be evaluated in scalar context and its value returned.
102  
103 This is a short-circuiting operator; if the hash does contain the key then the
104 righthand side expression is not evaluated at all.
105  
106 This operator parses at the same precedence level as the logical-or operators
107 (C<||> and C<//>).
108  
109 =head2 existsor
110  
111 do {
112 $hash{$key} existsor EXPR;
113 };
114  
115 Similar to the C<\\> operator but parses at the same level as the
116 low-precedence or operator (C<or>). This is unlikely to be very useful, as
117 normally C<or> would be used for value-less control flow. Such a potential use
118 for this operator would be neater written
119  
120 exists $hash{$key} or EXPR;
121  
122 It is included largely for completeness.
123  
124 =cut
125  
126 =head1 AUTHOR
127  
128 Paul Evans <leonerd@leonerd.org.uk>
129  
130 =cut
131  
132 1 0x55AA;
↑ Summary hax/OP_HELEMEXISTSOR.c.inc »