I have a function that depends on the context of the call, and I wanted to use this function as an argument for another function. Surprisingly, I found that this function secondis called in the context of the list now. I tried scalar context with help +(), but it does not work as I expected. Thus, the only way is to implicitly call it scalar.
use 5.010;
say first( 1, second( 'y' ) );
say first( 1, +( second( 'y' ) ) );
say first( 1, scalar second( 'y' ) );
sub first {
my $x = shift;
my $y = shift;
return "$x + $y";
}
sub second {
my $y = shift;
if ( wantarray ) {
qw/ array array /;
} else {
'scalar';
}
}
__END__
1 + array
1 + array
1 + scalar
Function arguments are treated as a list, but does this mean that each argument in this list also implies a list context? If so, why?
And, using scalarworks, but what other ways should I call this function in a scalar context (without an intermediate variable)?