note
gone2015
<p>As [JavaFan] says, contrary to most expectations <c>\</c> actually provides <c>LIST</c> context ! So:<code>
#!/usr/bin/perl -w
use strict ; use warnings ;
sub foo {
my ($tag) = @_ ;
print "$tag: ", wantarray ? "LIST" : "SCALAR", "\n" ;
return wantarray ? ('a', 'b') : \78 ;
} ;
my $a = \foo('SCALAR \foo()') ;
my @a = \foo('LIST \foo()') ;
my $b = ${foo('SCALAR ${foo()}')} ;
my @b = (${foo('LIST ${foo()}')}) ;
</code>gives:<pre>
SCALAR \foo(): LIST
LIST \foo(): LIST
SCALAR ${foo()}: SCALAR
LIST ${foo()}: SCALAR
</pre>which also shows that <c>${...}</c> does provide <c>SCALAR</c> context as expected.</p>
<p>This behaviour is not mentioned in [doc://perlop] and I cannot see it in [doc://perlref] either :-(</p>
<p>It is documented that <c>\($a, @b, %c)</c> is the same as <c>(\$a, \@b, \%c)</c>. Which requires the <c>,</c> to be operating in <c>LIST</c> context -- so perhaps we should not be surprised. (It's also worth remembering that <c>LIST</c>s are interpolated when they are <u>evaluated</u>, not when they are <u>constructed</u>...)</p>
759475
759480