To most programmers, 0 means false. This is the case in most programming languages. But, did you know, that somewhere “0 but true” is a thing? Can you guess what language that is? This language is also often attributed to as the Duct Tape of the Internet.
I’ve been programming for many years. Maybe not all that much in the last decade, but I still do, and I still learn new ones from time to time. Last week, I had to pick up Lua, and found myself a little thrown off by some of its idiosyncrasies. Then, I remember this other language that I use a lot. It wasn’t the first half-dozen or so languages I learnt, nor what I use in my most intense development work, but I’ve spent much of my working life using it.
This language is the Duct Tape of the Internet. It’s also where 0 is usually false, but can be true when you need it to.
This is Perl. It’s a scripting language, first appearing in 1987. It is referred to as the Duct Tape of the Internet because many system administrators and network administrators use Perl scripts extensively to manage network and server systems. Perl is used to generate configurations, manipulate data, integrate between systems, and pretty much that glue that holds everything together so that they can work. The Internet works because of Perl. The Internet would fall apart without Perl.
I am reminded about how others might understandably by horrified by Perl. When they look Perl code, one might be bewildered about what they’re seeing. It’s like a mishmash of languages: it kind of looks like shell script, yet also a bit of C, and then what is it with all the regular expressions and awk-like syntax?
You know how Singaporeans speak Singlish, our version of English that mixes in a bunch of Mandarin (and dialects), Malay, and Tamil phrases, along with new grammar and other syntactic oddities that foreigners find hard to grasp. Perl is like that. A mashup of languages. You think you see C in it, but yet you also see shell, and perhaps what might be shorthand for C, shell, and awk. You think it must have been made up, but no, it is real.
Sidenote: These days some may argue that Perl’s status as the Duct Tape of the Internet is being challenged. It’s old, and in many cases, newer people are preferring to use Python instead.
0 but true comes from an interesting need when functions in Perl want to return a value (an integer), which could be 0 or more, while also needing to return an indication of an error. An example of this is with Perl database functions such as the DBI do method, which wants to return the number of rows affected by a statement. If an error occurred, it also needs to be indicated. Ordinarily, it returns the number of rows affected, which could be 0. So here’s the problem, if 0 is “false” as with many languages, what happens when the function wants to indicate 0 but no error?
In other languages, one might choose to return “-1” to indicate an error, while 0 or more means all is good. The problem is that “-1” is true, and while you could test for the “-1” value, it just wasn’t elegant. Besides, with the example above of the DBI’s do method, “-1” is also a no-error condition that means the number of rows affected is not known, not available, or not applicable.
So, Perl made “0 but true” a thing, and its actual value is “0e0”. As a number in exponential notation, it evaluates to 0, and in the context of a logical expression, it evaluates to true. Hence, 0 but true. Example code:
$a = 0e0;
print "$a, ", $a + 1, "\n";
if (a) { print "true\n"; } else { print "false\n"; }
Output is:
0, 1
true
It’s a fun language that trips up newbies.
So that brings me to Lua. It also has some oddities. Other languages may have their quirks too, but I find it worth mentioning a few about Lua, because they make you stop and think about your fundamental understanding of programming concepts.
For example, I wanted to write some if/then/else code that involved multiple logical operations. It’s not difficult, really, but I thought I would quickly reference the documentation to confirm “A and B” would be what it should be. It has to right?
Oh dear. I googled. And this is how “and” and “or” are defined.
The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument.
I was stunned for a while. Then reading further, I found that “not 0” is false. I learned that in Lua, only false and nil are false, anything else is true. Hence, 0 being “anything else”, it is true, and thus “not 0” is false.
Oh, you see why that brought back memories of Perl’s 0 but true. Lua took it a step further, 0 is true.
Yet, that really isn’t something so extraordinary. For people who work in Unix environment, we don’t have to look very far to find that Unix shells also typically evaluate 0 as true. It comes about because a program’s exit code of 0 means “success” or “all is good”, while a non-zero exit code means “something went wrong”.
#!/bin/sh
if [ 0 ]; then
echo true
fi
That above will output “true” because 0 is true. A good reminder to not assume that 0 is always false.
Let me return to the definition of and and or in Lua. If you take a moment to digest the definition provided above, you’ll find that the and and or operators do still produce the desired behaviour of what we think and and or ought to do. The unexpected definition aside, there’s nothing unusual with Lua per se. However, the way it handles such operations allow us to use some convenient idioms in Lua. (I’ll leave that as an exercise for you to find out.)
Another unusual thing about Lua is about arrays. First, it does not have arrays. Instead, you’re supposed to use associative arrays, which are called tables in Lua. You just use numbers as the key in the table to represent the array index. I can live with that. I’d assume the numbers, used for the array index, should start from 0 right? Oh… no, no, no. In Lua, it is customary to start with 1, because the Lua libraries are designed to assume you start with 1. You could start with any other number if you so wished, including 0, but that will just make using the standard libraries a bit more tedious.
In which other language do you know where array indices do not begin with 0?
Right, maybe you might know Fortran. They too use 1. One of the rare exceptions.
I thought computers and programmers start numbering everything from 0.
FWIW, I’m trying to use Lua because it’s the scripting language used to customise some behaviour in Slurm. This isn’t my first encounter with Lua. The Vera Plus Advanced home automation controller I had used 8 years ago also made use of Lua, though at that time I merely just made very trivial edits to simple code snippets which didn’t require much understanding of the language.
I didn’t come back to Lua again until last week. Now that I try to actually understand Lua, I’m amused by the idiosyncrasies present in not just Lua, but also reminded about how they are also happening in other languages one way or other. It makes learning programming fascinating.
View Comment Policy