Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does STDIN here work in a list context?

Depending on the context we get either a scalar or an array. Ok so far.
But in the following:

print reverse <STDIN>;

Why do I get a list context? I mean reverse according to doc is either a list or a scalar context. So is print.

Prints a string or a list of strings. Returns true if successful.

So is STDIN. So why does STDIN here retrieve lines until EOF and not just collect the first line?

like image 998
Cratylus Avatar asked Dec 16 '22 11:12

Cratylus


1 Answers

You seem to be conflating two independent things:

  • An operator is evaluated in list, scalar or void context.
  • An operator decides in which context its operands are evaluated.

The operands of reverse are always evaluated in list context.

reverse LIST

So <STDIN> will be evaluated in list context.


Like all operators that can return something other than a scalar, reverse behaves differently in scalar context and in list context.

The operands of print are always evaluated in list context.

print LIST

So reverse will be evaluated in list context. That means it will reverse the order of its operands. It won't reverse the order of the characters of each operand, and it won't concatenate the list.

like image 118
ikegami Avatar answered Jan 08 '23 08:01

ikegami