As a Perl fossil I recognise this syntax as equivalent to if(not@myarray) which does the same thing. And here I was thinking Guido had deliberately aimed to avoid Perlisms in Python.
That said, the Perlism in question is the right* way to do it in Perl. The length operator does not do the expected thing on an array variable. (You get the length of the stringified length of the array. And a warning if those are enabled.)
* You can start a fight with modern Perl hackers with whether unless(@myarray) is better or just plain wrong, even if it works and is equivalent.
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 warningsFATAL=> "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.
I really liked unless in perl; especially as I get older !length or something makes that bang really easy to miss. I use !(length) or something instead to visually set it aside. unless made this much more visually clear.
Empty sequences being false goes back a lot further than perl, it was already a thing in the first lisp (in fact the empty list was the cannonical false).
As a Perl fossil I recognise this syntax as equivalent to
if(not @myarray)
which does the same thing. And here I was thinking Guido had deliberately aimed to avoid Perlisms in Python.That said, the Perlism in question is the right* way to do it in Perl. The
length
operator does not do the expected thing on an array variable. (You get the length of the stringified length of the array. And a warning if those are enabled.)* You can start a fight with modern Perl hackers with whether
unless(@myarray)
is better or just plain wrong, even if it works and is equivalent.That’s absolutely cursed.
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 bylength
yet, andlength
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”.
it’s the string coercion that I have a problem with. I’d much rather have an error than have things silently be coerced to different types.
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.I really liked
unless
in perl; especially as I get older!length
or something makes that bang really easy to miss. I use!(length)
or something instead to visually set it aside.unless
made this much more visually clear.Empty sequences being false goes back a lot further than perl, it was already a thing in the first lisp (in fact the empty list was the cannonical false).