Why function arguments induce list context?

perl

Solution

Why function arguments induce list context?

Subroutines accept a variable number of scalars as arguments. What other choice is there?

Arguments to function are treated as list, but does it mean that every argument in that list implies list context too? If yes, then why?

Yes. Because you want to be able to build lists from the contents of hashes and arrays. There's a million reason why that's useful.

%h = (%h, ...);  # Add to a hash
f( $x, @opts );  # Composing argument lists
etc

using `scalar` works, but which other ways i have to call this function in scalar context (without intermediate variable)?

Kinda.

say first( 1, "".second( 'y' ) );    # Side-effect: stringification
say first( 1, 0+.second( 'y' ) );    # Side-effect: numificatiion
say first( 1, !!second( 'y' ) );     # Side-effect: conversion to boolean

Subrountine prototypes can also enforce scalar context, but they're generally seen as bad for that very reason.

Problem

I have a function, which depends on calling context and i wanted to use this function as as argument to other function. Surprisingly i discovered that this `second` function is called in list context now. I tried force scalar context with `+()` but it does not work as i expected. So only way was to call it implicitly with `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 ``` Arguments to function are treated as list, but does it mean that every argument in that list implies list context too? If yes, then why? And, using `scalar` works, but which other ways i have to call this function in scalar context (without intermediate variable)?

Original source