I'll be speaking at the 2014 German Perl Workshop, so I hope to see some of you there.

Ben Tilly posted the following on Facebook a few days ago (I've modified it every so slightly to make the possible answers clearer):

@ar1 = qw(foo bar); @ar2 = qw(a b c d); print scalar (@ar1, @ar2);

He argued that even experienced developers will often get this wrong. Looking at the above, we could possibly argue for any of the following to be printed:

1

2

3

4

6

d

foo

Try and guess the answer without consulting the docs or reading any of the responses! I'll give a rationale for each of those after the cut (and for the record, I got the answer wrong the first time), but I won't be giving the answer -- I assume that will show up pretty quickly in the comments. Note that, of course, the following rationales are wrong (badly so in some cases), but this gives you an idea of how difficult context can be in Perl.

1

Due to the comma being a list operator, the two arrays are first flattened into a list and either the first or last element is returned, either of which in scalar context evaluates to 1.

2

Reasoning: @ar1 has two elements and scalar() takes an array and evaluates it in scalar context.

3

Due to the comma being a list operator, the two arrays are first flattened into a list and the first element, foo , is returned, which evaluates to 3 in scalar context.

4

All of the arrays are evaluated in scalar context, returning a list of 2, 4 , but only the last element of the list is returned by scalar() .

6

The two arrays, combined, have six elements, which evaluated as a scalar, causes 6 to be printed.

d

Due to the comma being a list operator, the two arrays are first flattened into a list and the last element is returned, which in scalar context is itself.

foo

Due to the comma being a list operator, the two arrays are first flattened into a list and the first element is returned, which in scalar context is itself.

Discuss!

Note: if you want to be really picky, according to a strict reading of the documentation, while one of the answers above is right, none of the rationales are. Have fun!