blib/lib/Wanted.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 290 CC 52 SCAR 46.8
CC
52
Cov
0%
_wantone
272.0
wantref
132.0
context
72.0
CRAP 108.2
1 ##----------------------------------------------------------------------------
2 ## Wanted - ~/lib/Wanted.pm
3 ## Version v0.1.1
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2025/05/16
7 ## Modified 2026/06/08
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Wanted;
15 use strict;
16 use warnings;
17 require Exporter;
18 require DynaLoader;
19 our @ISA = qw( Exporter DynaLoader );
20 our @EXPORT = qw( want rreturn lnoreturn );
21 our @EXPORT_OK = qw( context howmany wantref );
22 our $VERSION = 'v0.1.1';
23 our $DEBUG;
24  
25 bootstrap Wanted $VERSION;
26  
27 my %reftype = (
28 ARRAY => 1,
29 HASH => 1,
30 CODE => 1,
31 GLOB => 1,
32 OBJECT => 1,
33 );
34  
35 sub bump_level
36 {
37 my( $level ) = @_;
38 for(;;)
39 {
40 my( $p, $r ) = parent_op_name( $level + 1 );
41 if( !defined( $p ) )
42 {
43 # Return undef if parent_op_name fails (outside subroutine)
44 return;
45 }
46 if( $p eq 'return' ||
47 $p eq '(none)' && $r =~ /^leavesub(lv)?$/ )
48 {
49 ++$level
50 }
51 else
52 {
53 return( $level );
54 }
55 }
56 }
57  
58 sub context
59 {
60 my $gimme = wantarray_up(1);
61 return( 'VOID' ) unless( defined( $gimme ) );
62 my $ref_type = wantref(2);
63 if( $ref_type )
64 {
65 return( $ref_type eq 'SCALAR' ? 'REFSCALAR' : $ref_type );
66 }
67 # Boolean must come before scalar
68 elsif( want_boolean( bump_level(1) ) )
69 {
70 return('BOOL');
71 }
72 elsif( !!wantassign(2) )
73 {
74 return( 'ASSIGN' );
75 }
76 elsif( $gimme )
77 {
78 return( 'LIST' );
79 }
80 elsif( $gimme == 0 )
81 {
82 return( 'SCALAR' );
83 }
84 # Should not happen
85 else
86 {
87 return( '' );
88 }
89 }
90  
91 sub double_return :lvalue;
92  
93 sub howmany ()
94 {
95 my $level = bump_level( @_, 1 );
96 # Return undef if bump_level fails
97 return unless( defined( $level ) );
98 my $count = want_count( $level );
99 return( $count < 0 ? undef : $count );
100 }
101  
102 sub want
103 {
104 if( @_ == 1 && $_[0] eq 'ASSIGN' )
105 {
106 @_ = (1);
107 goto &wantassign;
108 }
109 want_uplevel( 1, @_ );
110 }
111  
112 sub want_uplevel
113 {
114 my( $level, @args ) = @_;
115  
116 if( 1 == @args )
117 {
118 @_ = ( 1 + $level );
119 goto &wantref if( $args[0] eq 'REF' );
120 goto &howmany if( $args[0] eq 'COUNT' );
121 goto &wantassign if( $args[0] eq 'ASSIGN' );
122 }
123  
124 for my $arg ( map split, @args )
125 {
126 my $is_neg = substr( $arg, 0, 1 ) eq '!';
127 if( substr( $arg, 0, 1 ) eq '!' )
128 {
129 $is_neg = 1;
130 $arg = substr( $arg, 1 );
131 }
132 my $result = _wantone( 2 + $level, $arg );
133 # Return undef if context is invalid
134 return unless( defined( $result ) );
135 return(0) if( ( !$is_neg && !$result ) || ( $is_neg && $result ) );
136 }
137 return(1);
138 }
139  
140 sub wantassign
141 {
142 my $uplevel = shift( @_ );
143 return unless( want_lvalue( $uplevel ) );
144 my $r = want_assign( bump_level( $uplevel ) );
145 if( want('BOOL') )
146 {
147 return( defined( $r ) && $r != 0 );
148 }
149 else
150 {
151 return( $r ? ( want('SCALAR') ? $r->[ $#$r ] : @$r ) : () );
152 }
153 }
154  
155 sub wantref
156 {
157 my $level = bump_level( @_, 1 );
158 # Return undef if bump_level fails
159 return unless( defined( $level ) );
160 my $n = parent_op_name( $level );
161 return unless( defined( $n ) );
162 if( $n eq 'rv2av' )
163 {
164 return( 'ARRAY' );
165 }
166 elsif( $n eq 'rv2hv' )
167 {
168 return( 'HASH' );
169 }
170 elsif( $n eq 'rv2cv' || $n eq 'entersub' )
171 {
172 return( 'CODE' );
173 # Address issue No 47963: want() Confused by Prototypes (Jul 17, 2009)
174 # Not working... Need to modify the XS code.
175 }
176 elsif( $n eq 'rv2gv' || $n eq 'gelem' )
177 {
178 return( 'GLOB' );
179 }
180 elsif( $n eq 'rv2sv' )
181 {
182 return( 'SCALAR' );
183 }
184 elsif( $n eq 'method_call' )
185 {
186 return( 'OBJECT' );
187 }
188 elsif( $n eq 'multideref' )
189 {
190 if( $] >= 5.022000 )
191 {
192 return( first_multideref_type( $level ) );
193 }
194 return( '' );
195 }
196 else
197 {
198 return( '' );
199 }
200 }
201  
202 sub rreturn(@)
203 {
204 if( want_lvalue(1) )
205 {
206 die( "Can't rreturn in lvalue context" );
207 }
208  
209 {
210 return( double_return( @_ ) );
211 }
212 }
213  
214 sub lnoreturn () : lvalue
215 {
216 if( !want_lvalue(1) || !want_assign(1) )
217 {
218 die( "Can't lnoreturn except in ASSIGN context" );
219 }
220  
221 if( $] >= 5.019 )
222 {
223 return( double_return( disarm_temp( my $undef ) ) );
224 }
225 return( double_return( disarm_temp( my $undef ) ) );
226 }
227  
228 sub _wantone
229 {
230 my( $uplevel, $arg ) = @_;
231
232 my $wantref = wantref( $uplevel + 1 );
233 if( $arg =~ /^\d+$/ )
234 {
235 my $want_count = want_count( $uplevel );
236 return( $want_count == -1 || $want_count >= $arg );
237 }
238 elsif( lc( $arg ) eq 'infinity' )
239 {
240 return( want_count( $uplevel ) == -1 );
241 }
242 elsif( $arg eq 'REF' )
243 {
244 return( $wantref );
245 }
246 elsif( $reftype{ $arg } )
247 {
248 no warnings; # If $wantref is undef
249 return( $wantref eq $arg );
250 }
251 elsif( $arg eq 'REFSCALAR' )
252 {
253 no warnings; # If $wantref is undef
254 return( $wantref eq 'SCALAR' );
255 }
256 elsif( $arg eq 'LVALUE' )
257 {
258 return( want_lvalue( $uplevel ) );
259 }
260 elsif( $arg eq 'RVALUE' )
261 {
262 return( !want_lvalue( $uplevel ) );
263 }
264 elsif( $arg eq 'VOID' )
265 {
266 return( !defined( wantarray_up( $uplevel ) ) );
267 }
268 elsif( $arg eq 'SCALAR' )
269 {
270 my $gimme = wantarray_up( $uplevel );
271 # Return undef if context is invalid
272 return unless( defined( $gimme ) );
273 return( $gimme == 0 );
274 }
275 elsif( $arg eq 'BOOL' || $arg eq 'BOOLEAN' )
276 {
277 return( want_boolean( bump_level( $uplevel ) ) );
278 }
279 elsif( $arg eq 'LIST' )
280 {
281 my $gimme = wantarray_up( $uplevel );
282 # Return undef if context is invalid
283 return unless( defined( $gimme ) );
284 return( $gimme );
285 }
286 elsif( $arg eq 'COUNT' )
287 {
288 die( "want: COUNT must be the *only* parameter" );
289 }
290 elsif( $arg eq 'ASSIGN' )
291 {
292 return( !!wantassign( $uplevel + 1 ) );
293 }
294 else
295 {
296 die( "want: Unrecognised specifier $arg" );
297 }
298 }
299  
300 *_wantref = \&wantref;
301  
302 *_wantassign = \&wantassign;
303  
304 1;
305 # NOTE: POD
306 __END__