elif’s missing ‘e’

@aran384 complains on Twitter about Perl’s elsif, throwing up his hands with a “WTF?”. It’s the sort of facile complaint by the people who can’t think or don’t want to think.


In general, things are the way they are for a reason. It might not be a good reason, but there’s usually a reason. Thinking it’s just stupid is a bad starting point. That the sort of middle school thinking university is supposed to beat out of you.

There’s a problem in C, which doesn’t require braces around the body of an if for a single line:

#include 

int main( void ) {
	int x = 6;
	
	if( x > 10 )
		printf( "It's greater than 10\n" );
	else if( x == 10 )
		printf( "It's equal to 10\n" );	
	else
		printf( "It's less than 10\n" );
	
	return 0;
	}

That branch in the middle is two keywords, else followed immediately by an if. That leads a particular class of mistake where two unrelated branching bits can turn into one. Start with some code that you expect to always output two lines, one from each branch:

#include 

int main( void ) {
	int x = 6;
	
	if( x > 10 )
		printf( "It's greater than 10\n" );
	else 
		printf( "It's not greater than 10\n" );
	
	
	if( x == 10 )
		printf( "It's equal to 10\n" );	
	else
		printf( "It's not equal to 10\n" );
	
	return 0;
	}

Somewhere along the way you edit the code to remove a statement under the first else, but you forget to replace it with another statement:

#include 

int main( void ) {
	int x = 6;
	
	if( x > 10 )
		printf( "It's greater than 10" );
	else 
		/* printf( "It's not greater than 10" ); */
	
	
	if( x == 10 )
		printf( "It's equal to 10" );	
	else
		printf( "It's not equal to 10" );
	
	return 0;
	}

That compiles without a warning and now you only get one line of output! That one of the reasons that all such bodies in Perl require braces, but even then missing braces can make the two run together.

Thus, Larry made elsif, a single keyword. He could have made it elseif, but that would have had an i after and e and not in the “neighbor” and “weigh” way. This offended his linguistic training so he got rid of the e. At one conference he talked about this issue and said if you wanted the e you could make your own language.

Many things in Perl are like this, and we talk about these things in Learning Perl (sometimes in footnotes). Most odd things like this in Perl are a reaction to such problems. If you want to understand the language, you should ask “Why?”, sincerely, instead of “WTF?”, indignantly. But, that’s a critical thinking skill that you shouldn’t limit to Perl.

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.