Hello everyone. I was just wondering if it is OK to do this:

if ($beer == $wine) {# Some code}
elsif ($beer == $vodka) {}
else {die("Oh dear!  Neither condition satisfied!")}

i.e. have an empty block attached to an if, elsif or else. It might sound like an odd question, but I don't want to put undefined behaviour into my program by breaking some obscure rule. Any advice appriciated.

Steven.

technically, as far as I know, it is OK. In general I would say it's not good practice unless it's clear why you needed to do that or put comments in the code explaining why you are doing that. Like in the CGI module you will see a sub routine that is just an empty block:

# avoid autoloader warnings
sub DESTROY {}

as the comment notes it's just there to avoid a warning message. In your case make sure it's really necessary, there is probably a better way to do whatever it is you are doing but can't tell by the sample code you posted.

Thanks Kevin. Actually, I have decided to release the application I am working on (perlBB) as open source software. So, if you want to have a peek at the code you can visit the project home page on Sourceforge (http://sourceforge.net/projects/perlbb ). The if....else construct I mentioned is in "sub update_list" in /perlbb_0_4/forum.pl.

Steven.

It is perfectly ok what compiler regards, but I agree it is not a good coding style.

Hi,

you should be aware that using '==' you can compare numbers, not strings. Strings however can be compared with 'eq', 'ne' etc.

Svet

Thanks for the advice guys. I have rewritten the part of my program that contained the empty block to avoid it, as I agree its not good aesthetically.

Steven.

I would say it's not good practice unless it's clear why you needed to do that or put comments in the code explaining why you are doing that.

This article has been dead for over six months. Start a new discussion instead.