I think you're missing my point. It does seem to consistently evaluate the arguments on both sides in a SCALAR context if it's an EXPR and not an actual variable name (similar to how (say) keys requires something literally begining with a % to work upon). That means that your constant sub is being evaluated in scalar context returning the final element of the LIST, just as everywhere else you have a true LIST in scalar context.
1 #!/usr/local/bin/perl5.10.0
2 use feature ":5.10";
3
4 BEGIN {
5 sub ctx () {
6 my @caller = caller(1);
7 say "$caller[3]:$caller[2] wantarray: ",
8 defined $caller[5]
9 ? ( $caller[5] ? "yes" : "no" )
10 : "undef";
11 }
12
13 sub ret_scalar () { ctx; "a" }
14 sub ret_list () { ctx; @{[ qw/a b c/ ]} }
15 }
16
17 ret_scalar;
18 my $a = ret_scalar;
19 my @a = ret_list;
20
21 my $list_in_scalar = ret_list;
22 say "list in scalar: ", $list_in_scalar;
23
24 my @scalar_in_list = ret_scalar;
25 say "scalar in list: ", join( ", ", @scalar_in_list );
26
27 say "match" if ret_scalar ~~ ret_list;
28 say "match" if ret_list ~~ ret_scalar;
29 say "match" if ret_scalar ~~ [ ret_list ];
30
31 __END__
main::ret_scalar:17 wantarray: undef
main::ret_scalar:18 wantarray: no
main::ret_list:19 wantarray: yes
main::ret_list:21 wantarray: no
list in scalar: 3
main::ret_scalar:24 wantarray: yes
scalar in list: a
main::ret_scalar:27 wantarray: no
main::ret_list:27 wantarray: no
main::ret_list:28 wantarray: no
main::ret_scalar:28 wantarray: no
main::ret_scalar:29 wantarray: no
main::ret_list:29 wantarray: yes
match
I will grant you that I couldn't find documentation in my quick once over explicitly documenting that EXPRs would be evaluated in scalar context. In any case the quick fix is to either change your constant sub to return an arrayref or wrap the call to the constant sub in an anonymous arrayref constructor.
Update: Duur, as is pointed out below it's a binary operator so that's probably why it's not explicitly spelled out.
Update the second: Tweaked ret_list to behave more like the sub generated by the OP's constant call than my initial version's sub ret_list () { ctx; qw/a b c/ } did; still doesn't match because of the scalar context imposed but the exact value that doesn't match is 3 rather than 'c'.
The cake is a lie.
The cake is a lie.
The cake is a lie.
|