in reply to Re: Printing from stdin to stdout
in thread Printing from stdin to stdout

Wow! That explains it. I didnt know that print creates a list context.

Thank you very much for your quick answer on a Sunday evening!!

Replies are listed 'Best First'.
Re^3: Printing from stdin to stdout
by haukex (Archbishop) on Aug 26, 2019 at 07:37 UTC

    Note that a simple way to force scalar context on anything is scalar, as in print scalar <STDIN>;.

      Also note that while there is no similar keyword for forcing list context, it can be done with @{[]}, as in print @{[<STDIN>]}, but actually using that is an advanced feature and if you force list context in a scalar context, you will get the number of items in the list rather than any of the values in list.

        if you force list context in a scalar context, you will get the number of items in the list rather than any of the values in list

        For the @{[...]} form you showed, that's correct, because it's an array, not a list - but more generally, the usual behavior of lists and arrays in scalar context applies. As documented in scalar:

        There is no equivalent operator to force an expression to be interpolated in list context because in practice, this is never needed. If you really wanted to do so, however, you could use the construction @{[ (some expression) ]}, but usually a simple (some expression) suffices.

        Because scalar is a unary operator, if you accidentally use a parenthesized list for the EXPR, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context. This is seldom what you want.