

Well, you see, Perl’s length
is only for strings and if you want the length of an array, you use @arrayname
itself in scalar context.
Now, length
happens to provide scalar context to its right hand side, so @arrayname
already returns the required length. Unfortunately, at that point it hasn’t been processed by length
yet, and length
requires a string. And so, the length of the array is coerced to be a string and then the length of that string is returned.
A case of “don’t order fries if your meal already comes with them or you’ll end up with too many fries”.
Perl was originally designed to carry on regardless, and that remains its blessing and curse, a bit like JavaScript which came later.
Unlike JavaScript, if you really want it to throw a warning or even bail out completely at compiling such constructs (at least some of the time, like this one) it’s pretty easy to turn that on rather than resort to an entirely different language.
use warnings;
at the top of a program and it will punt a warning to STDERR as it carries merrily along.Make that
use warnings FATAL => "syntax";
and things that are technically valid but semantically weird like this will throw the error early and also prevent the program from running in the first place.