| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
##---------------------------------------------------------------------------- |
|
2
|
|
|
|
|
|
|
## Wanted - ~/lib/Wanted.pm |
|
3
|
|
|
|
|
|
|
## Version v0.1.0 |
|
4
|
|
|
|
|
|
|
## Copyright(c) 2025 DEGUEST Pte. Ltd. |
|
5
|
|
|
|
|
|
|
## Author: Jacques Deguest <jack@deguest.jp> |
|
6
|
|
|
|
|
|
|
## Created 2025/05/16 |
|
7
|
|
|
|
|
|
|
## Modified 2025/05/24 |
|
8
|
|
|
|
|
|
|
## All rights reserved |
|
9
|
|
|
|
|
|
|
## |
|
10
|
|
|
|
|
|
|
## This program is free software; you can redistribute it and/or modify it |
|
11
|
|
|
|
|
|
|
## under the same terms as Perl itself. |
|
12
|
|
|
|
|
|
|
##---------------------------------------------------------------------------- |
|
13
|
|
|
|
|
|
|
package Wanted; |
|
14
|
12
|
|
|
12
|
|
580092
|
use strict; |
|
|
12
|
|
|
|
|
25
|
|
|
|
12
|
|
|
|
|
453
|
|
|
15
|
12
|
|
|
12
|
|
55
|
use warnings; |
|
|
12
|
|
|
|
|
23
|
|
|
|
12
|
|
|
|
|
18790
|
|
|
16
|
|
|
|
|
|
|
require Exporter; |
|
17
|
|
|
|
|
|
|
require DynaLoader; |
|
18
|
|
|
|
|
|
|
our @ISA = qw( Exporter DynaLoader ); |
|
19
|
|
|
|
|
|
|
our @EXPORT = qw( want rreturn lnoreturn ); |
|
20
|
|
|
|
|
|
|
our @EXPORT_OK = qw( context howmany wantref ); |
|
21
|
|
|
|
|
|
|
our $VERSION = 'v0.1.0'; |
|
22
|
|
|
|
|
|
|
our $DEBUG; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
bootstrap Wanted $VERSION; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my %reftype = ( |
|
27
|
|
|
|
|
|
|
ARRAY => 1, |
|
28
|
|
|
|
|
|
|
HASH => 1, |
|
29
|
|
|
|
|
|
|
CODE => 1, |
|
30
|
|
|
|
|
|
|
GLOB => 1, |
|
31
|
|
|
|
|
|
|
OBJECT => 1, |
|
32
|
|
|
|
|
|
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub bump_level |
|
35
|
|
|
|
|
|
|
{ |
|
36
|
316
|
|
|
316
|
0
|
480
|
my( $level ) = @_; |
|
37
|
316
|
|
|
|
|
349
|
for(;;) |
|
38
|
|
|
|
|
|
|
{ |
|
39
|
346
|
|
|
|
|
1838
|
my( $p, $r ) = parent_op_name( $level + 1 ); |
|
40
|
346
|
50
|
|
|
|
724
|
if( !defined( $p ) ) |
|
41
|
|
|
|
|
|
|
{ |
|
42
|
|
|
|
|
|
|
# Return undef if parent_op_name fails (outside subroutine) |
|
43
|
0
|
|
|
|
|
0
|
return; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
346
|
100
|
66
|
|
|
2429
|
if( $p eq 'return' || |
|
|
|
|
66
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$p eq '(none)' && $r =~ /^leavesub(lv)?$/ ) |
|
47
|
|
|
|
|
|
|
{ |
|
48
|
30
|
|
|
|
|
62
|
++$level |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
else |
|
51
|
|
|
|
|
|
|
{ |
|
52
|
316
|
|
|
|
|
775
|
return( $level ); |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub context |
|
58
|
|
|
|
|
|
|
{ |
|
59
|
11
|
|
|
11
|
1
|
5829
|
my $gimme = wantarray_up(1); |
|
60
|
11
|
100
|
|
|
|
39
|
return( 'VOID' ) unless( defined( $gimme ) ); |
|
61
|
10
|
|
|
|
|
24
|
my $ref_type = wantref(2); |
|
62
|
10
|
100
|
|
|
|
23
|
if( $ref_type ) |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
{ |
|
64
|
7
|
100
|
|
|
|
23
|
return( $ref_type eq 'SCALAR' ? 'REFSCALAR' : $ref_type ); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
# Boolean must come before scalar |
|
67
|
|
|
|
|
|
|
elsif( want_boolean( bump_level(1) ) ) |
|
68
|
|
|
|
|
|
|
{ |
|
69
|
1
|
|
|
|
|
3
|
return('BOOL'); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
elsif( !!wantassign(2) ) |
|
72
|
|
|
|
|
|
|
{ |
|
73
|
0
|
|
|
|
|
0
|
return( 'ASSIGN' ); |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
elsif( $gimme ) |
|
76
|
|
|
|
|
|
|
{ |
|
77
|
1
|
|
|
|
|
4
|
return( 'LIST' ); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
elsif( $gimme == 0 ) |
|
80
|
|
|
|
|
|
|
{ |
|
81
|
1
|
|
|
|
|
3
|
return( 'SCALAR' ); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
# Should not happen |
|
84
|
|
|
|
|
|
|
else |
|
85
|
|
|
|
|
|
|
{ |
|
86
|
0
|
|
|
|
|
0
|
return( '' ); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub double_return :lvalue; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub howmany () |
|
93
|
|
|
|
|
|
|
{ |
|
94
|
18
|
|
|
18
|
1
|
1275
|
my $level = bump_level( @_, 1 ); |
|
95
|
|
|
|
|
|
|
# Return undef if bump_level fails |
|
96
|
18
|
50
|
|
|
|
37
|
return unless( defined( $level ) ); |
|
97
|
18
|
|
|
|
|
96
|
my $count = want_count( $level ); |
|
98
|
18
|
100
|
|
|
|
60
|
return( $count < 0 ? undef : $count ); |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub want |
|
102
|
|
|
|
|
|
|
{ |
|
103
|
209
|
100
|
100
|
209
|
1
|
1226947
|
if( @_ == 1 && $_[0] eq 'ASSIGN' ) |
|
104
|
|
|
|
|
|
|
{ |
|
105
|
13
|
|
|
|
|
22
|
@_ = (1); |
|
106
|
13
|
|
|
|
|
27
|
goto &wantassign; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
196
|
|
|
|
|
441
|
want_uplevel( 1, @_ ); |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub want_uplevel |
|
112
|
|
|
|
|
|
|
{ |
|
113
|
196
|
|
|
196
|
0
|
435
|
my( $level, @args ) = @_; |
|
114
|
|
|
|
|
|
|
|
|
115
|
196
|
100
|
|
|
|
396
|
if( 1 == @args ) |
|
116
|
|
|
|
|
|
|
{ |
|
117
|
171
|
|
|
|
|
1434
|
@_ = ( 1 + $level ); |
|
118
|
171
|
100
|
|
|
|
367
|
goto &wantref if( $args[0] eq 'REF' ); |
|
119
|
166
|
100
|
|
|
|
1372
|
goto &howmany if( $args[0] eq 'COUNT' ); |
|
120
|
151
|
50
|
|
|
|
293
|
goto &wantassign if( $args[0] eq 'ASSIGN' ); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
176
|
|
|
|
|
597
|
for my $arg ( map split, @args ) |
|
124
|
|
|
|
|
|
|
{ |
|
125
|
200
|
|
|
|
|
371
|
my $is_neg = substr( $arg, 0, 1 ) eq '!'; |
|
126
|
200
|
100
|
|
|
|
437
|
if( substr( $arg, 0, 1 ) eq '!' ) |
|
127
|
|
|
|
|
|
|
{ |
|
128
|
29
|
|
|
|
|
33
|
$is_neg = 1; |
|
129
|
29
|
|
|
|
|
52
|
$arg = substr( $arg, 1 ); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
200
|
|
|
|
|
381
|
my $result = _wantone( 2 + $level, $arg ); |
|
132
|
|
|
|
|
|
|
# Return undef if context is invalid |
|
133
|
200
|
100
|
|
|
|
412
|
return unless( defined( $result ) ); |
|
134
|
197
|
100
|
100
|
|
|
2197
|
return(0) if( ( !$is_neg && !$result ) || ( $is_neg && $result ) ); |
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} |
|
136
|
86
|
|
|
|
|
339
|
return(1); |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub wantassign |
|
140
|
|
|
|
|
|
|
{ |
|
141
|
21
|
|
|
21
|
1
|
31
|
my $uplevel = shift( @_ ); |
|
142
|
21
|
100
|
|
|
|
89
|
return unless( want_lvalue( $uplevel ) ); |
|
143
|
14
|
|
|
|
|
16
|
my $r = want_assign( bump_level( $uplevel ) ); |
|
144
|
14
|
100
|
|
|
|
25
|
if( want('BOOL') ) |
|
145
|
|
|
|
|
|
|
{ |
|
146
|
9
|
|
66
|
|
|
36
|
return( defined( $r ) && $r != 0 ); |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
else |
|
149
|
|
|
|
|
|
|
{ |
|
150
|
5
|
100
|
|
|
|
12
|
return( $r ? ( want('SCALAR') ? $r->[ $#$r ] : @$r ) : () ); |
|
|
|
50
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub wantref |
|
155
|
|
|
|
|
|
|
{ |
|
156
|
226
|
|
|
226
|
1
|
426019
|
my $level = bump_level( @_, 1 ); |
|
157
|
|
|
|
|
|
|
# Return undef if bump_level fails |
|
158
|
226
|
50
|
|
|
|
429
|
return unless( defined( $level ) ); |
|
159
|
226
|
|
|
|
|
905
|
my $n = parent_op_name( $level ); |
|
160
|
226
|
50
|
|
|
|
538
|
return unless( defined( $n ) ); |
|
161
|
226
|
100
|
100
|
|
|
1425
|
if( $n eq 'rv2av' ) |
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
{ |
|
163
|
16
|
|
|
|
|
36
|
return( 'ARRAY' ); |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
elsif( $n eq 'rv2hv' ) |
|
166
|
|
|
|
|
|
|
{ |
|
167
|
18
|
|
|
|
|
37
|
return( 'HASH' ); |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
elsif( $n eq 'rv2cv' || $n eq 'entersub' ) |
|
170
|
|
|
|
|
|
|
{ |
|
171
|
11
|
|
|
|
|
27
|
return( 'CODE' ); |
|
172
|
|
|
|
|
|
|
# Address issue No 47963: want() Confused by Prototypes (Jul 17, 2009) |
|
173
|
|
|
|
|
|
|
# Not working... Need to modify the XS code. |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
elsif( $n eq 'rv2gv' || $n eq 'gelem' ) |
|
176
|
|
|
|
|
|
|
{ |
|
177
|
4
|
|
|
|
|
12
|
return( 'GLOB' ); |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
elsif( $n eq 'rv2sv' ) |
|
180
|
|
|
|
|
|
|
{ |
|
181
|
2
|
|
|
|
|
6
|
return( 'SCALAR' ); |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
elsif( $n eq 'method_call' ) |
|
184
|
|
|
|
|
|
|
{ |
|
185
|
3
|
|
|
|
|
11
|
return( 'OBJECT' ); |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
elsif( $n eq 'multideref' ) |
|
188
|
|
|
|
|
|
|
{ |
|
189
|
7
|
50
|
|
|
|
34
|
if( $] >= 5.022000 ) |
|
190
|
|
|
|
|
|
|
{ |
|
191
|
7
|
|
|
|
|
42
|
return( first_multideref_type( $level ) ); |
|
192
|
|
|
|
|
|
|
} |
|
193
|
0
|
|
|
|
|
0
|
return( '' ); |
|
194
|
|
|
|
|
|
|
} |
|
195
|
|
|
|
|
|
|
else |
|
196
|
|
|
|
|
|
|
{ |
|
197
|
165
|
|
|
|
|
330
|
return( '' ); |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub rreturn(@) |
|
202
|
|
|
|
|
|
|
{ |
|
203
|
15
|
100
|
|
15
|
1
|
603063
|
if( want_lvalue(1) ) |
|
204
|
|
|
|
|
|
|
{ |
|
205
|
3
|
|
|
|
|
57
|
die( "Can't rreturn in lvalue context" ); |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
{ |
|
209
|
12
|
|
|
|
|
26
|
return( double_return( @_ ) ); |
|
|
12
|
|
|
|
|
67
|
|
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub lnoreturn () : lvalue |
|
214
|
|
|
|
|
|
|
{ |
|
215
|
12
|
100
|
66
|
12
|
1
|
5603
|
if( !want_lvalue(1) || !want_assign(1) ) |
|
216
|
|
|
|
|
|
|
{ |
|
217
|
3
|
|
|
|
|
29
|
die( "Can't lnoreturn except in ASSIGN context" ); |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
|
|
220
|
9
|
50
|
|
|
|
27
|
if( $] >= 5.019 ) |
|
221
|
|
|
|
|
|
|
{ |
|
222
|
9
|
|
|
|
|
42
|
return( double_return( disarm_temp( my $undef ) ) ); |
|
223
|
|
|
|
|
|
|
} |
|
224
|
0
|
|
|
|
|
0
|
return( double_return( disarm_temp( my $undef ) ) ); |
|
225
|
|
|
|
|
|
|
} |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub _wantone |
|
228
|
|
|
|
|
|
|
{ |
|
229
|
200
|
|
|
200
|
|
389
|
my( $uplevel, $arg ) = @_; |
|
230
|
|
|
|
|
|
|
|
|
231
|
200
|
|
|
|
|
355
|
my $wantref = wantref( $uplevel + 1 ); |
|
232
|
200
|
100
|
66
|
|
|
3444
|
if( $arg =~ /^\d+$/ ) |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
{ |
|
234
|
17
|
|
|
|
|
85
|
my $want_count = want_count( $uplevel ); |
|
235
|
17
|
|
100
|
|
|
69
|
return( $want_count == -1 || $want_count >= $arg ); |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
elsif( lc( $arg ) eq 'infinity' ) |
|
238
|
|
|
|
|
|
|
{ |
|
239
|
6
|
|
|
|
|
68
|
return( want_count( $uplevel ) == -1 ); |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
elsif( $arg eq 'REF' ) |
|
242
|
|
|
|
|
|
|
{ |
|
243
|
3
|
|
|
|
|
11
|
return( $wantref ); |
|
244
|
|
|
|
|
|
|
} |
|
245
|
|
|
|
|
|
|
elsif( $reftype{ $arg } ) |
|
246
|
|
|
|
|
|
|
{ |
|
247
|
12
|
|
|
12
|
|
120
|
no warnings; # If $wantref is undef |
|
|
12
|
|
|
|
|
22
|
|
|
|
12
|
|
|
|
|
832
|
|
|
248
|
42
|
|
|
|
|
92
|
return( $wantref eq $arg ); |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
elsif( $arg eq 'REFSCALAR' ) |
|
251
|
|
|
|
|
|
|
{ |
|
252
|
12
|
|
|
12
|
|
65
|
no warnings; # If $wantref is undef |
|
|
12
|
|
|
|
|
32
|
|
|
|
12
|
|
|
|
|
7215
|
|
|
253
|
6
|
|
|
|
|
16
|
return( $wantref eq 'SCALAR' ); |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
elsif( $arg eq 'LVALUE' ) |
|
256
|
|
|
|
|
|
|
{ |
|
257
|
19
|
|
|
|
|
1114
|
return( want_lvalue( $uplevel ) ); |
|
258
|
|
|
|
|
|
|
} |
|
259
|
|
|
|
|
|
|
elsif( $arg eq 'RVALUE' ) |
|
260
|
|
|
|
|
|
|
{ |
|
261
|
7
|
|
|
|
|
34
|
return( !want_lvalue( $uplevel ) ); |
|
262
|
|
|
|
|
|
|
} |
|
263
|
|
|
|
|
|
|
elsif( $arg eq 'VOID' ) |
|
264
|
|
|
|
|
|
|
{ |
|
265
|
4
|
|
|
|
|
18
|
return( !defined( wantarray_up( $uplevel ) ) ); |
|
266
|
|
|
|
|
|
|
} |
|
267
|
|
|
|
|
|
|
elsif( $arg eq 'SCALAR' ) |
|
268
|
|
|
|
|
|
|
{ |
|
269
|
19
|
|
|
|
|
65
|
my $gimme = wantarray_up( $uplevel ); |
|
270
|
|
|
|
|
|
|
# Return undef if context is invalid |
|
271
|
19
|
100
|
|
|
|
44
|
return unless( defined( $gimme ) ); |
|
272
|
17
|
|
|
|
|
48
|
return( $gimme == 0 ); |
|
273
|
|
|
|
|
|
|
} |
|
274
|
|
|
|
|
|
|
elsif( $arg eq 'BOOL' || $arg eq 'BOOLEAN' ) |
|
275
|
|
|
|
|
|
|
{ |
|
276
|
55
|
|
|
|
|
110
|
return( want_boolean( bump_level( $uplevel ) ) ); |
|
277
|
|
|
|
|
|
|
} |
|
278
|
|
|
|
|
|
|
elsif( $arg eq 'LIST' ) |
|
279
|
|
|
|
|
|
|
{ |
|
280
|
16
|
|
|
|
|
50
|
my $gimme = wantarray_up( $uplevel ); |
|
281
|
|
|
|
|
|
|
# Return undef if context is invalid |
|
282
|
16
|
100
|
|
|
|
43
|
return unless( defined( $gimme ) ); |
|
283
|
15
|
|
|
|
|
33
|
return( $gimme ); |
|
284
|
|
|
|
|
|
|
} |
|
285
|
|
|
|
|
|
|
elsif( $arg eq 'COUNT' ) |
|
286
|
|
|
|
|
|
|
{ |
|
287
|
0
|
|
|
|
|
0
|
die( "want: COUNT must be the *only* parameter" ); |
|
288
|
|
|
|
|
|
|
} |
|
289
|
|
|
|
|
|
|
elsif( $arg eq 'ASSIGN' ) |
|
290
|
|
|
|
|
|
|
{ |
|
291
|
6
|
|
|
|
|
21
|
return( !!wantassign( $uplevel + 1 ) ); |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
else |
|
294
|
|
|
|
|
|
|
{ |
|
295
|
0
|
|
|
|
|
|
die( "want: Unrecognised specifier $arg" ); |
|
296
|
|
|
|
|
|
|
} |
|
297
|
|
|
|
|
|
|
} |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
*_wantref = \&wantref; |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
*_wantassign = \&wantassign; |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
1; |
|
304
|
|
|
|
|
|
|
# NOTE: POD |
|
305
|
|
|
|
|
|
|
__END__ |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=encoding utf-8 |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 NAME |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Wanted - Extended caller context detection |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
use Wanted; |
|
316
|
|
|
|
|
|
|
sub foo :lvalue |
|
317
|
|
|
|
|
|
|
{ |
|
318
|
|
|
|
|
|
|
if ( want( qw'LVALUE ASSIGN' ) ) |
|
319
|
|
|
|
|
|
|
{ |
|
320
|
|
|
|
|
|
|
print "We have been assigned ", want('ASSIGN'); |
|
321
|
|
|
|
|
|
|
lnoreturn; |
|
322
|
|
|
|
|
|
|
} |
|
323
|
|
|
|
|
|
|
elsif( want('LIST') ) |
|
324
|
|
|
|
|
|
|
{ |
|
325
|
|
|
|
|
|
|
rreturn (1, 2, 3); |
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
elsif( want('BOOL') ) |
|
328
|
|
|
|
|
|
|
{ |
|
329
|
|
|
|
|
|
|
rreturn 0; |
|
330
|
|
|
|
|
|
|
} |
|
331
|
|
|
|
|
|
|
elsif( want(qw'SCALAR !REF') ) |
|
332
|
|
|
|
|
|
|
{ |
|
333
|
|
|
|
|
|
|
rreturn 23; |
|
334
|
|
|
|
|
|
|
} |
|
335
|
|
|
|
|
|
|
elsif( want('HASH') ) |
|
336
|
|
|
|
|
|
|
{ |
|
337
|
|
|
|
|
|
|
rreturn { foo => 17, bar => 23 }; |
|
338
|
|
|
|
|
|
|
} |
|
339
|
|
|
|
|
|
|
# You have to put this at the end to keep the compiler happy |
|
340
|
|
|
|
|
|
|
return; |
|
341
|
|
|
|
|
|
|
} |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
foo() = 23; # Assign context |
|
344
|
|
|
|
|
|
|
@x = foo(); # List context |
|
345
|
|
|
|
|
|
|
if( foo() ) # Boolean context |
|
346
|
|
|
|
|
|
|
{ |
|
347
|
|
|
|
|
|
|
print( "Not reached\n" ); |
|
348
|
|
|
|
|
|
|
} |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
Also works in threads, where the context is set at thread creation. |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
require threads; |
|
353
|
|
|
|
|
|
|
# In scalar context |
|
354
|
|
|
|
|
|
|
my $thr = threads->create(sub |
|
355
|
|
|
|
|
|
|
{ |
|
356
|
|
|
|
|
|
|
return( want('SCALAR') ); |
|
357
|
|
|
|
|
|
|
}); |
|
358
|
|
|
|
|
|
|
my $is_scalar = $thr->join; # true |
|
359
|
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# or |
|
361
|
|
|
|
|
|
|
my $thr = threads->create({ context => 'scalar' }, sub |
|
362
|
|
|
|
|
|
|
{ |
|
363
|
|
|
|
|
|
|
return( want('SCALAR') ); |
|
364
|
|
|
|
|
|
|
}); |
|
365
|
|
|
|
|
|
|
my $is_scalar = $thr->join; # true |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
my( $thr ) = threads->create(sub |
|
368
|
|
|
|
|
|
|
{ |
|
369
|
|
|
|
|
|
|
return( want('LIST') ); |
|
370
|
|
|
|
|
|
|
}); |
|
371
|
|
|
|
|
|
|
my @list_result = $thr->join; |
|
372
|
|
|
|
|
|
|
# $list_result[0] is true |
|
373
|
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
# or |
|
375
|
|
|
|
|
|
|
my $thr = threads->create({ context => 'list' }, sub |
|
376
|
|
|
|
|
|
|
{ |
|
377
|
|
|
|
|
|
|
return( want('LIST') ); |
|
378
|
|
|
|
|
|
|
}); |
|
379
|
|
|
|
|
|
|
my @list_result = $thr->join; |
|
380
|
|
|
|
|
|
|
# $list_result[0] is true |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
# Force the context by being explicit: |
|
383
|
|
|
|
|
|
|
my $thr = threads->create({ context => 'void' }, sub |
|
384
|
|
|
|
|
|
|
{ |
|
385
|
|
|
|
|
|
|
return( want('VOID') ? 1 : 0 ); |
|
386
|
|
|
|
|
|
|
}); |
|
387
|
|
|
|
|
|
|
my $is_void = $thr->join; # true |
|
388
|
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=head1 VERSION |
|
390
|
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
v0.1.0 |
|
392
|
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
394
|
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
This XS module generalises the mechanism of the L<wantarray|perlfunc/wantarray> function, allowing a function to determine in detail how its return value is going to be used. |
|
396
|
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
It is a fork from the module L<Want>, by Robin Houston, that is not updated anymore since 2016, and that throws a segmentation fault when called from the last line of a thread, or from a tie method, or from the last line of a mod_perl handler, when there is a lack of context. |
|
398
|
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
To install this module, run the following commands: |
|
400
|
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
perl Makefile.PL |
|
402
|
|
|
|
|
|
|
make |
|
403
|
|
|
|
|
|
|
make test |
|
404
|
|
|
|
|
|
|
make install |
|
405
|
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
=head2 Top-level contexts: |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
The three kinds of top-level context are well known: |
|
409
|
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=over 4 |
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
=item B<VOID> |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
The return value is not being used in any way. It could be an entire statement like C<foo();>, or the last component of a compound statement which is itself in void context, such as C<$test || foo();>n. Be warned that the last statement of a subroutine will be in whatever context the subroutine was called in, because the result is implicitly returned. |
|
415
|
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=item B<SCALAR> |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
The return value is being treated as a scalar value of some sort: |
|
419
|
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
my $x = foo(); |
|
421
|
|
|
|
|
|
|
$y += foo(); |
|
422
|
|
|
|
|
|
|
print "123" x foo(); |
|
423
|
|
|
|
|
|
|
print scalar foo(); |
|
424
|
|
|
|
|
|
|
warn foo()->{23}; |
|
425
|
|
|
|
|
|
|
# ...etc... |
|
426
|
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
=item B<LIST> |
|
428
|
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
The return value is treated as a list of values: |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
my @x = foo(); |
|
432
|
|
|
|
|
|
|
my ($x) = foo(); |
|
433
|
|
|
|
|
|
|
() = foo(); # even though the results are discarded |
|
434
|
|
|
|
|
|
|
print foo(); |
|
435
|
|
|
|
|
|
|
bar(foo()); # unless the bar subroutine has a prototype |
|
436
|
|
|
|
|
|
|
print @hash{foo()}; # (hash slice) |
|
437
|
|
|
|
|
|
|
# ...etc... |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
=back |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
=head2 Lvalue subroutines: |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
The introduction of B<lvalue subroutines> in Perl 5.6 has created a new type of contextual information, which is independent of those listed above. When an lvalue subroutine is called, it can either be called in the ordinary way (so that its result is treated as an ordinary value, an B<rvalue>); or else it can be called so that its result is considered updatable, an B<lvalue>. |
|
444
|
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
These rather arcane terms (lvalue and rvalue) are easier to remember if you know why they are so called. If you consider a simple assignment statement C<left = right>, then the B<l>eft-hand side is an B<l>value and the B<r>ight-hand side is an B<r>value. |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
So (for lvalue subroutines only) there are two new types of context: |
|
448
|
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
=over 4 |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=item B<RVALUE> |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
The caller is definitely not trying to assign to the result: |
|
454
|
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
foo(); |
|
456
|
|
|
|
|
|
|
my $x = foo(); |
|
457
|
|
|
|
|
|
|
# ...etc... |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
If the sub is declared without the C<:lvalue> attribute, then it will I<always> be in RVALUE context. |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
If you need to return values from an lvalue subroutine in RVALUE context, you should use the C<rreturn> function rather than an ordinary C<return>. Otherwise you will probably get a compile-time error in perl 5.6.1 and later. |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
=item B<LVALUE> |
|
464
|
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
Either the caller is directly assigning to the result of the sub call: |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
foo() = $x; |
|
468
|
|
|
|
|
|
|
foo() = (1, 1, 2, 3, 5, 8); |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
or the caller is making a reference to the result, which might be assigned to later: |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
my $ref = \(foo()); # Could now have: $$ref = 99; |
|
473
|
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
# Note that this example imposes LIST context on the sub call. |
|
475
|
|
|
|
|
|
|
# So we are taking a reference to the first element to be |
|
476
|
|
|
|
|
|
|
# returned _in list context_. |
|
477
|
|
|
|
|
|
|
# If we want to call the function in scalar context, we can |
|
478
|
|
|
|
|
|
|
# do it like this: |
|
479
|
|
|
|
|
|
|
my $ref = \(scalar foo()); |
|
480
|
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
or else the result of the function call is being used as part of the argument list for I<another> function call: |
|
482
|
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
bar(foo()); # Will *always* call foo in lvalue context, |
|
484
|
|
|
|
|
|
|
# (provided that foo is an C<:lvalue> sub) |
|
485
|
|
|
|
|
|
|
# regardless of what bar actually does. |
|
486
|
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
The reason for this last case is that bar might be a sub which modifies its arguments. They are rare in contemporary Perl code, but perfectly possible: |
|
488
|
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
sub bar { |
|
490
|
|
|
|
|
|
|
$_[0] = 23; |
|
491
|
|
|
|
|
|
|
} |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
(This is really a throwback to Perl 4, which did not support explicit references.) |
|
494
|
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
=back |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
=head2 Assignment context: |
|
498
|
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
The commonest use of lvalue subroutines is with the assignment statement: |
|
500
|
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
size() = 12; |
|
502
|
|
|
|
|
|
|
(list()) = (1..10); |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
A useful motto to remember when thinking about assignment statements is I<context comes from the left>. Consider code like this: |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
my ($x, $y, $z); |
|
507
|
|
|
|
|
|
|
sub list () :lvalue { ($x, $y, $z) } |
|
508
|
|
|
|
|
|
|
list = (1, 2, 3); |
|
509
|
|
|
|
|
|
|
print "\$x = $x; \$y = $y; \$z = $z\n"; |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
This prints C<$x = ; $y = ; $z = 3>, which may not be what you were expecting. The reason is that the assignment is in scalar context, so the comma operator is in scalar context too, and discards all values but the last. You can fix it by writing C<(list) = (1,2,3);> instead. |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
If your lvalue subroutine is used on the left of an assignment statement, it is in B<ASSIGN> context. If ASSIGN is the only argument to C<want()>, then it returns a reference to an array of the value(s) of the right-hand side. |
|
514
|
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
In this case, you should return with the C<lnoreturn> function, rather than an ordinary L<return|perlfunc/return>. |
|
516
|
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
This makes it very easy to write lvalue subroutines which do clever things: |
|
518
|
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
use Wanted; |
|
520
|
|
|
|
|
|
|
use strict; |
|
521
|
|
|
|
|
|
|
sub backstr :lvalue { |
|
522
|
|
|
|
|
|
|
if (want(qw'LVALUE ASSIGN')) { |
|
523
|
|
|
|
|
|
|
my ($a) = want('ASSIGN'); |
|
524
|
|
|
|
|
|
|
$_[0] = reverse $a; |
|
525
|
|
|
|
|
|
|
lnoreturn; |
|
526
|
|
|
|
|
|
|
} |
|
527
|
|
|
|
|
|
|
elsif (want('RVALUE')) { |
|
528
|
|
|
|
|
|
|
rreturn scalar reverse $_[0]; |
|
529
|
|
|
|
|
|
|
} |
|
530
|
|
|
|
|
|
|
else { |
|
531
|
|
|
|
|
|
|
carp("Not in ASSIGN context"); |
|
532
|
|
|
|
|
|
|
} |
|
533
|
|
|
|
|
|
|
return |
|
534
|
|
|
|
|
|
|
} |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
print "foo -> ", backstr("foo"), "\n"; # foo -> oof |
|
537
|
|
|
|
|
|
|
backstr(my $robin) = "nibor"; |
|
538
|
|
|
|
|
|
|
print "\$robin is now $robin\n"; # $robin is now robin |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
Notice that you need to put a (meaningless) return statement at the end of the function, otherwise you will get the |
|
541
|
|
|
|
|
|
|
error I<Can't modify non-lvalue subroutine call in lvalue subroutine return>. |
|
542
|
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
The only way to write that C<backstr> function without using Want is to return a tied variable which is tied to a custom class. |
|
544
|
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=head2 Reference context: |
|
546
|
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
Sometimes in scalar context the caller is expecting a reference of some sort to be returned: |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
print foo()->(); # CODE reference expected |
|
550
|
|
|
|
|
|
|
print foo()->{bar}; # HASH reference expected |
|
551
|
|
|
|
|
|
|
print foo()->[23]; # ARRAY reference expected |
|
552
|
|
|
|
|
|
|
print ${foo()}; # SCALAR reference expected |
|
553
|
|
|
|
|
|
|
print foo()->bar(); # OBJECT reference expected |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
my $format = *{foo()}{FORMAT} # GLOB reference expected |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
You can check this using conditionals like C<if (want('CODE'))>. |
|
558
|
|
|
|
|
|
|
There is also a function C<wantref()> which returns one of the strings C<CODE>, C<HASH>, C<ARRAY>, C<GLOB>, C<SCALAR> or C<OBJECT>; or the empty string if a reference is not expected. |
|
559
|
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
Because C<want('SCALAR')> is already used to select ordinary scalar context, you have to use C<want('REFSCALAR')> to find out if a SCALAR reference is expected. Or you could use C<want('REF') eq 'SCALAR'> of course. |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
Be warned that C<want('ARRAY')> is a B<very> different thing from C<wantarray()>. |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
=head2 Item count |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
Sometimes in list context the caller is expecting a particular number of items to be returned: |
|
567
|
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
my ($x, $y) = foo(); # foo is expected to return two items |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
If you pass a number to the C<want> function, then it will return true or false according to whether at least that many items are wanted. So if we are in the definition of a sub which is being called as above, then: |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
want(1) returns true |
|
573
|
|
|
|
|
|
|
want(2) returns true |
|
574
|
|
|
|
|
|
|
want(3) returns false |
|
575
|
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
Sometimes there is no limit to the number of items that might be used: |
|
577
|
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
my @x = foo(); |
|
579
|
|
|
|
|
|
|
do_something_with( foo() ); |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
In this case, C<want(2)>, C<want(100)>, C<want(1E9)> and so on will all return true; and so will C<want('Infinity')>. |
|
582
|
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
The C<howmany> function can be used to find out how many items are wanted. |
|
584
|
|
|
|
|
|
|
If the context is scalar, then C<want(1)> returns true and C<howmany()> returns 1. |
|
585
|
|
|
|
|
|
|
If you want to check whether your result is being assigned to a singleton list, you can say C<if (want('LIST', 1)) { ... }>. |
|
586
|
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
=head2 Boolean context |
|
588
|
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
Sometimes the caller is only interested in the truth or falsity of a function's return value: |
|
590
|
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
if (everything_is_okay()) { |
|
592
|
|
|
|
|
|
|
# Carry on |
|
593
|
|
|
|
|
|
|
} |
|
594
|
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
print (foo() ? "ok\n" : "not ok\n"); |
|
596
|
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
In the following example, all subroutine calls are in BOOL context: |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
my $x = ( (foo() && !bar()) xor (baz() || quux()) ); |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
Boolean context, like the reference contexts above, is considered to be a subcontext of C<SCALAR>. |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
=head2 want(SPECIFIERS) |
|
606
|
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
This is the primary interface to this module, and should suffice for most purposes. You pass it a list of context specifiers, and the return value is true whenever all of the specifiers hold. |
|
608
|
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
want('LVALUE', 'SCALAR'); # Are we in scalar lvalue context? |
|
610
|
|
|
|
|
|
|
want('RVALUE', 3); # Are at least three rvalues wanted? |
|
611
|
|
|
|
|
|
|
want('ARRAY'); # Is the return value used as an array ref? |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
You can also prefix a specifier with an exclamation mark to indicate that you B<do not> want it to be true |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
want(2, '!3'); # Caller wants exactly two items. |
|
616
|
|
|
|
|
|
|
want(qw'REF !CODE !GLOB'); # Expecting a reference that is not a CODE or GLOB ref. |
|
617
|
|
|
|
|
|
|
want(100, '!Infinity'); # Expecting at least 100 items, but there is a limit. |
|
618
|
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
If the I<REF> keyword is the only parameter passed, then the type of reference will be returned. This is just a synonym for the C<wantref> function: it is included because you might find it useful if you do not want to pollute your namespace by importing several functions, and to conform to L<Damian Conway's suggestion in RFC 21|http://dev.perl.org/rfc/21.html>. |
|
620
|
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
Finally, the keyword C<COUNT> can be used, provided that it is the only keyword you pass. Mixing C<COUNT> with other keywords is an error. This is a synonym for the L</howmany> function. |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
A full list of the permitted keyword is in the L</ARGUMENTS> section below. |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=head2 rreturn |
|
626
|
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
Use this function instead of L<return|perlfunc/return> from inside an lvalue subroutine when you know that you are in C<RVALUE> context. If you try to use a normal L<return|perlfunc/return>, you will get a compile-time error in Perl 5.6.1 and above unless you return an lvalue. (Note: this is no longer true in Perl 5.16, where an ordinary return will once again work.) |
|
628
|
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
B<C<rreturn> inside C<eval>:> In Perl 5.36 and later, C<rreturn> may fail to detect lvalue context inside an C<eval> block due to a Perl core limitation (see L</Detection of Lvalue Context Inside C<eval>>). This can lead to incorrect behaviour, as the necessary stack context is not properly propagated. For example: |
|
630
|
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
eval { lvalue_sub() = 42 }; # lvalue context not detected, rreturn may not die as expected |
|
632
|
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
B<Recommendation:> Avoid using C<rreturn> inside an C<eval> block when the subroutine is in lvalue context. Instead, move the lvalue operation outside the C<eval> or explicitly handle the context in your subroutine logic. |
|
634
|
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
=head2 lnoreturn |
|
636
|
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
Use this function instead of C<return> from inside an lvalue subroutine when you are in C<ASSIGN> context and you have used C<want('ASSIGN')> to carry out the appropriate action. |
|
638
|
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
If you use L</rreturn> or L</lnoreturn>, then you have to put a bare C<return;> at the very end of your lvalue subroutine, in order to stop the Perl compiler from complaining. Think of it as akin to the C<1;> that you have to put at the end of a module. (Note: this is no longer true in Perl 5.16.) |
|
640
|
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
=head2 howmany() |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Returns the I<expectation count>, i.e. the number of items expected. If the expectation count is undefined, that indicates that an unlimited number of items might be used (e.g. the return value is being assigned to an array). In void context the expectation count is zero, and in scalar context it is one. |
|
644
|
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
The same as C<want('COUNT')>. |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
=head2 wantref() |
|
648
|
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
Returns the type of reference which the caller is expecting, or the empty string if the caller is not expecting a reference immediately. |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
The same as C<want('REF')>. |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=head2 context |
|
654
|
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
=over 4 |
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
=item * C<context()> |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
Returns a string representing the current calling context, such as C<VOID>, C<SCALAR>, C<LIST>, C<BOOL>, C<CODE>, C<HASH>, C<ARRAY>, C<GLOB>, C<REFSCALAR>, C<ASSIGN>, or C<OBJECT>. This function provides a convenient way to determine the context without manually checking multiple conditions using L</want>. |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=over 4 |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
=item * Arguments |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
None. |
|
666
|
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
=item * Returns |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
A string indicating the current context, such as C<VOID>, C<SCALAR>, C<LIST>, C<BOOL>, C<CODE>, C<HASH>, C<ARRAY>, C<GLOB>, C<REFSCALAR>, C<ASSIGN>, or C<OBJECT>. Returns C<VOID> if the context cannot be determined. |
|
670
|
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
=item * Example |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
sub test_context |
|
674
|
|
|
|
|
|
|
{ |
|
675
|
|
|
|
|
|
|
my $ctx = context(); |
|
676
|
|
|
|
|
|
|
print "Current context: $ctx\n"; |
|
677
|
|
|
|
|
|
|
} |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
test_context(); # Prints: Current context: VOID |
|
680
|
|
|
|
|
|
|
my $x = test_context(); # Prints: Current context: SCALAR |
|
681
|
|
|
|
|
|
|
my @x = test_context(); # Prints: Current context: LIST |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
=back |
|
684
|
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
=back |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=head1 INTERNAL FUNCTIONS |
|
688
|
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
The following functions are internal to C<Wanted> and are not intended for public use. They are documented here for reference but should not be relied upon in user code, as their behaviour or availability may change in future releases. |
|
690
|
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
=head2 wantassign |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
=over 4 |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
=item * C<wantassign($uplevel)> |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
Checks if the current context is an lvalue assignment context (C<ASSIGN>) at the specified C<$uplevel> in the call stack. Returns an array reference containing the values being assigned if in C<ASSIGN> context, or C<undef> if not. In boolean |
|
698
|
|
|
|
|
|
|
context (e.g., when C<want('BOOL')> is true), it returns a boolean indicating whether an assignment is occurring. |
|
699
|
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
This function is used internally by C<want('ASSIGN')> to handle lvalue assignments in subroutines marked with the C<:lvalue> attribute. |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
=over 4 |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
=item * Arguments |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
=over 4 |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
=item * C<$uplevel> |
|
709
|
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
An integer specifying how many levels up the call stack to check the context. |
|
711
|
|
|
|
|
|
|
Typically set to 1 to check the immediate caller. |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
=back |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
=item * Returns |
|
716
|
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
=over 4 |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
=item * In list or scalar context: An array reference containing the values from the right-hand side of the assignment, or C<undef> if not in C<ASSIGN> context. |
|
720
|
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
=item * In boolean context: A boolean indicating whether the context is an C<ASSIGN> context. |
|
722
|
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
=back |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=item * Example |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
sub assign_test :lvalue |
|
728
|
|
|
|
|
|
|
{ |
|
729
|
|
|
|
|
|
|
if( want('LVALUE', 'ASSIGN') ) |
|
730
|
|
|
|
|
|
|
{ |
|
731
|
|
|
|
|
|
|
my $values = wantassign(1); |
|
732
|
|
|
|
|
|
|
print "Assigned: @$values\n"; |
|
733
|
|
|
|
|
|
|
lnoreturn; |
|
734
|
|
|
|
|
|
|
} |
|
735
|
|
|
|
|
|
|
return; |
|
736
|
|
|
|
|
|
|
} |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
assign_test() = 42; # Prints: Assigned: 42 |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=back |
|
741
|
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
=back |
|
743
|
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
=head2 want_assign |
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
=over 4 |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
=item * C<want_assign($level)> |
|
749
|
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
A low-level XS function that retrieves the values being assigned in an lvalue assignment context (C<ASSIGN>) at the specified C<$level> in the call stack. |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
Returns an array reference containing the values from the right-hand side of the assignment, or C<undef> if not in an C<ASSIGN> context. |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
This function is used internally by L</wantassign> to fetch assignment values, which L<wantassign|/wantassign> then processes based on the caller's context (e.g., scalar, list, or boolean context). It supports C<want('ASSIGN')> indirectly through L<wantassign|/wantassign>. |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
=over 4 |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
=item * Arguments |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
=over 4 |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
=item * C<$level> |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
An integer specifying how many levels up the call stack to check the context. |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
=back |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
=item * Returns |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
=over 4 |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
=item * An array reference containing the values from the right-hand side of the assignment, or C<undef> if not in an C<ASSIGN> context. |
|
773
|
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
=back |
|
775
|
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
=item * Example |
|
777
|
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
This function is typically called by L</wantassign>, but for illustrative purposes: |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
sub assign_test :lvalue |
|
781
|
|
|
|
|
|
|
{ |
|
782
|
|
|
|
|
|
|
if( want('LVALUE', 'ASSIGN') ) |
|
783
|
|
|
|
|
|
|
{ |
|
784
|
|
|
|
|
|
|
my $values = want_assign( bump_level(1) ); |
|
785
|
|
|
|
|
|
|
print "Assigned: @$values\n"; |
|
786
|
|
|
|
|
|
|
lnoreturn; |
|
787
|
|
|
|
|
|
|
} |
|
788
|
|
|
|
|
|
|
return; |
|
789
|
|
|
|
|
|
|
} |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
assign_test() = 42; # Prints: Assigned: 42 |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
=back |
|
794
|
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
=back |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
=head2 want_boolean |
|
798
|
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
=over 4 |
|
800
|
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=item * C<want_boolean($level)> |
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
Checks if the context at the specified C<$level> in the call stack is a boolean context (C<BOOL>). Returns true if the caller is evaluating the return value as a boolean (e.g., in conditionals like C<if>, C<while>, or with logical operators like C<&&> or C<||>). |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
This function is used internally to support C<want('BOOL')>. |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=over 4 |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
=item * Arguments |
|
810
|
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
=over 4 |
|
812
|
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
=item * C<$level> |
|
814
|
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
An integer specifying how many levels up the call stack to check the context. |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
=back |
|
818
|
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
=item * Returns |
|
820
|
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
=over 4 |
|
822
|
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
=item * A boolean (true or false) indicating whether the context is a boolean context. |
|
824
|
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
=back |
|
826
|
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
=item * Example |
|
828
|
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
sub bool_test |
|
830
|
|
|
|
|
|
|
{ |
|
831
|
|
|
|
|
|
|
if( want_boolean(1) ) |
|
832
|
|
|
|
|
|
|
{ |
|
833
|
|
|
|
|
|
|
print "In boolean context\n"; |
|
834
|
|
|
|
|
|
|
return(1); |
|
835
|
|
|
|
|
|
|
} |
|
836
|
|
|
|
|
|
|
return(0); |
|
837
|
|
|
|
|
|
|
} |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
if( bool_test() ) |
|
840
|
|
|
|
|
|
|
{ |
|
841
|
|
|
|
|
|
|
# Prints: In boolean context |
|
842
|
|
|
|
|
|
|
} |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=back |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=back |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
=head2 want_count |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=over 4 |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=item * C<want_count($level)> |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
Returns the number of items expected by the caller at the specified C<$level> in the call stack. Used internally to support C<want('COUNT')> and L</howmany>. |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=over 4 |
|
857
|
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
=item * Arguments |
|
859
|
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
=over 4 |
|
861
|
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=item * C<$level> |
|
863
|
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
An integer specifying how many levels up the call stack to check the context. |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
=back |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=item * Returns |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
=over 4 |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
=item * An integer representing the number of items expected, or C<-1> if an unlimited number of items is expected (e.g., in list context with no fixed limit). |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
=back |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
=item * Example |
|
877
|
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
sub count_test |
|
879
|
|
|
|
|
|
|
{ |
|
880
|
|
|
|
|
|
|
my $count = want_count(1); |
|
881
|
|
|
|
|
|
|
print "Caller expects $count items\n"; |
|
882
|
|
|
|
|
|
|
} |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
my( $a, $b ) = count_test(); # Prints: Caller expects 2 items |
|
885
|
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
=back |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=back |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=head2 want_lvalue |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=over 4 |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
=item * C<want_lvalue($uplevel)> |
|
895
|
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
Checks if the context at the specified C<$uplevel> in the call stack is an lvalue context for a subroutine marked with the C<:lvalue> attribute. Returns true if the subroutine is called in a context where its return value can be assigned to. |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
This function is used internally to support C<want('LVALUE')> and C<want('RVALUE')>. |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=over 4 |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
=item * Arguments |
|
903
|
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
=over 4 |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
=item * C<$uplevel> |
|
907
|
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
An integer specifying how many levels up the call stack to check the context. |
|
909
|
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
=back |
|
911
|
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
=item * Returns |
|
913
|
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
=over 4 |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
=item * A boolean (true or false) indicating whether the context is an lvalue context. |
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
=back |
|
919
|
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
=item * Example |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
sub lvalue_test :lvalue |
|
923
|
|
|
|
|
|
|
{ |
|
924
|
|
|
|
|
|
|
if( want_lvalue(1) ) |
|
925
|
|
|
|
|
|
|
{ |
|
926
|
|
|
|
|
|
|
print "In lvalue context\n"; |
|
927
|
|
|
|
|
|
|
} |
|
928
|
|
|
|
|
|
|
my $var; |
|
929
|
|
|
|
|
|
|
} |
|
930
|
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
lvalue_test() = 42; # Prints: In lvalue context |
|
932
|
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
=back |
|
934
|
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
=back |
|
936
|
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
=head1 EXAMPLES |
|
938
|
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
use Wanted 'howmany'; |
|
940
|
|
|
|
|
|
|
sub numbers |
|
941
|
|
|
|
|
|
|
{ |
|
942
|
|
|
|
|
|
|
my $count = howmany(); |
|
943
|
|
|
|
|
|
|
die( "Cannot make an infinite list" ) if( !defined( $count ) ); |
|
944
|
|
|
|
|
|
|
return( 1..$count ); |
|
945
|
|
|
|
|
|
|
} |
|
946
|
|
|
|
|
|
|
my( $one, $two, $three ) = numbers(); |
|
947
|
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
use Wanted 'want'; |
|
949
|
|
|
|
|
|
|
sub pi () |
|
950
|
|
|
|
|
|
|
{ |
|
951
|
|
|
|
|
|
|
if( want('ARRAY') ) |
|
952
|
|
|
|
|
|
|
{ |
|
953
|
|
|
|
|
|
|
return( [3, 1, 4, 1, 5, 9] ); |
|
954
|
|
|
|
|
|
|
} |
|
955
|
|
|
|
|
|
|
elsif( want('LIST') ) |
|
956
|
|
|
|
|
|
|
{ |
|
957
|
|
|
|
|
|
|
return( 3, 1, 4, 1, 5, 9 ); |
|
958
|
|
|
|
|
|
|
} |
|
959
|
|
|
|
|
|
|
else |
|
960
|
|
|
|
|
|
|
{ |
|
961
|
|
|
|
|
|
|
return(3); |
|
962
|
|
|
|
|
|
|
} |
|
963
|
|
|
|
|
|
|
} |
|
964
|
|
|
|
|
|
|
print pi->[2]; # prints 4 |
|
965
|
|
|
|
|
|
|
print ((pi)[3]); # prints 1 |
|
966
|
|
|
|
|
|
|
|
|
967
|
|
|
|
|
|
|
=head1 ARGUMENTS |
|
968
|
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
The permitted arguments to the L<want|/want> function are listed below. |
|
970
|
|
|
|
|
|
|
The list is structured so that sub-contexts appear below the context that they are part of. |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
=over 4 |
|
973
|
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
=item * VOID |
|
975
|
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
=item * SCALAR |
|
977
|
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
=over 4 |
|
979
|
|
|
|
|
|
|
|
|
980
|
|
|
|
|
|
|
=item * REF |
|
981
|
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
=over 4 |
|
983
|
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
=item * REFSCALAR |
|
985
|
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
=item * CODE |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
=item * HASH |
|
989
|
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
=item * ARRAY |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
=item * GLOB |
|
993
|
|
|
|
|
|
|
|
|
994
|
|
|
|
|
|
|
=item * OBJECT |
|
995
|
|
|
|
|
|
|
|
|
996
|
|
|
|
|
|
|
=back |
|
997
|
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
=item * BOOL |
|
999
|
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
=back |
|
1001
|
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
=item * LIST |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
=over 4 |
|
1005
|
|
|
|
|
|
|
|
|
1006
|
|
|
|
|
|
|
=item * COUNT |
|
1007
|
|
|
|
|
|
|
|
|
1008
|
|
|
|
|
|
|
=item * E<lt>numberE<gt> |
|
1009
|
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
=item * Infinity |
|
1011
|
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
=back |
|
1013
|
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
=item * LVALUE |
|
1015
|
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
=over 4 |
|
1017
|
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
=item * ASSIGN |
|
1019
|
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
=back |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
=item * RVALUE |
|
1023
|
|
|
|
|
|
|
|
|
1024
|
|
|
|
|
|
|
=back |
|
1025
|
|
|
|
|
|
|
|
|
1026
|
|
|
|
|
|
|
=head1 EXPORT |
|
1027
|
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
The L</want> and L</rreturn> functions are exported by default. |
|
1029
|
|
|
|
|
|
|
|
|
1030
|
|
|
|
|
|
|
The L</wantref> and/or L</howmany> functions can also be imported: |
|
1031
|
|
|
|
|
|
|
|
|
1032
|
|
|
|
|
|
|
use Wanted qw( want howmany ); |
|
1033
|
|
|
|
|
|
|
|
|
1034
|
|
|
|
|
|
|
If you do not import these functions, you must qualify their names as (e.g.) C<Wanted::wantref>. |
|
1035
|
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
=head1 SUBTLETIES |
|
1037
|
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
There are two different levels of B<BOOL> context. I<Pure> boolean context occurs in conditional expressions, and the operands of the C<xor> and C<!>/C<not> operators. |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
Pure boolean context also propagates down through the C<&&> and C<||> operators. |
|
1041
|
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
However, consider an expression like C<my $x = foo() && "yes">. The subroutine is called in I<pseudo>-boolean context - its return value is not B<entirely> ignored, because the undefined value, the empty string and the integer C<0> are all false. |
|
1043
|
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
At the moment C<want('BOOL')> is true in either pure or pseudo boolean context. |
|
1045
|
|
|
|
|
|
|
|
|
1046
|
|
|
|
|
|
|
=head1 LIMITATIONS |
|
1047
|
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
=head2 Detection of Lvalue Context Inside C<eval> |
|
1049
|
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
Due to a known limitation in Perl's core behaviour, lvalue context cannot be reliably detected from within an C<eval> block in modern Perl versions (5.36 and later). |
|
1051
|
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
For example, the following will NOT be detected as lvalue context: |
|
1053
|
|
|
|
|
|
|
|
|
1054
|
|
|
|
|
|
|
eval { lvalue_sub() = 42 }; |
|
1055
|
|
|
|
|
|
|
|
|
1056
|
|
|
|
|
|
|
This occurs because Perl does not propagate lvalue context into the internal call frame used for C<eval>. As a result, C<want_lvalue()> will return false even though the subroutine is used in an lvalue assignment. |
|
1057
|
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
This limitation affects all XS-based context-detection modules, including L<Want>, and is not specific to C<Wanted>. It is a change in Perl's internals introduced in versions after 5.16, where the original L<Want> module was last updated. |
|
1059
|
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
B<Recommendation:> Avoid relying on lvalue context detection inside C<eval> blocks. Instead, move the assignment outside the C<eval>, or handle lvalue semantics explicitly in the subroutine logic. For example: |
|
1061
|
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
my $result = lvalue_sub(); |
|
1063
|
|
|
|
|
|
|
$result = 42; # Perform assignment outside eval |
|
1064
|
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
=head2 Code Reference Detection with Prototypes in Scalar Context |
|
1066
|
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
In scalar context, C<want('CODE')> may incorrectly return true when the caller does not expect a code reference, particularly when the subroutine has a prototype (e.g., C<sub foo($)>). This is a known issue (RT#47963) from the original L<Want> module, which has not been resolved in C<Wanted>. |
|
1068
|
|
|
|
|
|
|
|
|
1069
|
|
|
|
|
|
|
For example: |
|
1070
|
|
|
|
|
|
|
|
|
1071
|
|
|
|
|
|
|
sub foo($) { sub { } } # Prototype forces scalar context |
|
1072
|
|
|
|
|
|
|
my $x = foo(); # Scalar context, but want('CODE') returns true |
|
1073
|
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
In this case, C<want('CODE')> should return false because the caller does not expect a code reference, but it returns true due to limitations in context detection. |
|
1075
|
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
B<Recommendation:> Be cautious when using C<want('CODE')> in scalar context with prototyped subroutines. If necessary, explicitly check the context using C<want('SCALAR')> or avoid prototypes in such cases. |
|
1077
|
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
=head1 CREDITS |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
Robin Houston, E<lt>robin@cpan.orgE<gt> wrote the original module L<Want> on which this is based. |
|
1081
|
|
|
|
|
|
|
|
|
1082
|
|
|
|
|
|
|
Grok from L<xAI|https://x.ai> for its contribution on some XS code, providing unit tests to tackle edge cases, and help resolving several bugs from the original L<Want> module. |
|
1083
|
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
Albert (OpenAI) for its contribution on some XS code. |
|
1085
|
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
=head1 AUTHOR |
|
1087
|
|
|
|
|
|
|
|
|
1088
|
|
|
|
|
|
|
Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> |
|
1089
|
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
L<perlfunc/wantarray>, L<Perl6 RFC 21, by Damian Conway|http://dev.perl.org/rfc/21.html> |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
L<Contextual::Call>, L<Contextual::Diag>, L<Contextual::Return> |
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
Copyright(c) 2025 DEGUEST Pte. Ltd. |
|
1099
|
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
Portions copyright (c) 2001-2016, Robin Houston. |
|
1101
|
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
All rights reserved. |
|
1103
|
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
|
1105
|
|
|
|
|
|
|
|
|
1106
|
|
|
|
|
|
|
=cut |