List and scalar context

One of the things I love with Perl is how well it can tell when you want something to be in list or scalar context. However there are a couple times that the compiler isn’t sure which you want, and assumes incorrectly. This happened today to me on what seemed a very minimal issue. This was one of those cases where I typed a one-liner to confirm something, and the answer wasn’t what I expected and led me down the path of finding out what was wrong.

The basic problem was that I wanted to verify something with the ‘mday’ value out of the localtime() function, without capturing an entire array, or listing out undef in array locations when it assigned. so my one liner listed:

print localtime( time ) [3][1];

But this doesn’t work. So I figured I need to make sure it sees the list that localtime returns:

print ( localtime ( time ) ) [3][2];

And I got the same syntax error. Playing around I tried to assign it to a variable instead of printing:

$mday = ( localtime ( time ) ) [3];[3]

And that works.. Why?
The basic reason is that print actually expects a list, and the assignment was to an array, so the two really are different. Now, since in my real code I was doing an assignment anyways, that worked just fine. If by chance you actually wanted to print this without the capture though, just use the compiler hint of ‘+’ :

print +( localtime ( time ) ) [3];[4]

So, just remember kids, understand list and scalar context.

fn1. Print the 4th element of the localtime array as parsed from time()

fn2. Enforce the List context of the localtime function

fn3. Only difference is an assignment to a scalar instead of the print

fn4. The + in front just gives the parser/compiler a hint, or an anchor when it’s figuring out what you are really trying to do in what could be ambiguous circumstances.