Skip to main content

Posts

Showing posts from December 23, 2012

Saturday Perl lesson

What does this output?

print scalar (1, 4, 5)

If you said 3, that's incorrect. It's 5!

When 'scalar' is given a set of parentheses, every item except for the last one is evaluated in void context. Then the last item is used as 'scalar's argument.

Providing an array variable (or an array returned by a subroutine) to 'scalar' is not the same as providing it a set of parentheses. When an array variable is provided to 'scalar', the result is the number of items in the array. This outputs 3:

@a = (1,4,5);
print scalar @a;

Is this expected behavior? Of course not. It's Perl.

Winter 2012