Your return
is evaluated in list context because the function is called in list context. The boolean operators (like &&
) do not force scalar context.
To quote perlop on &&
:
Scalar or list context propagates down to the right operand if it is evaluated.
Since the match operator returns an empty list on failure in list context, that's what you get. If you want to force scalar context, use the scalar
operator, either in your return
statement, or when you call the function.
Also, the idea that parentheses force list context (as suggested by some of the other answers) is a common misconception. They don't. List context is the default; anything that could be list context is list context. That's why there's no list
operator that corresponds to scalar
. The one place where parens "force" list context is with the repetition operator. 'a' x 3
means 'aaa'
, but ('a') x 3
means ('a', 'a', 'a')
. But that's not actually forcing list context, it's just a special case for the x
operator.