| | |
Empty block in if....else construct. OK?
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hello everyone. I was just wondering if it is OK to do this:
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.
perl Syntax (Toggle Plain Text)
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.
Last edited by Mushy-pea; Dec 16th, 2006 at 3:47 pm.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
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:
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.
Perl Syntax (Toggle Plain Text)
# 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.
Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
You might want to read this:
http://perldoc.perl.org/perlstyle.html
http://perltidy.sourceforge.net/
http://perldoc.perl.org/perlstyle.html
http://perltidy.sourceforge.net/
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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
you should be aware that using '==' you can compare numbers, not strings. Strings however can be compared with 'eq', 'ne' etc.
Svet
![]() |
Similar Threads
- The G5 iMac Thread (Mac Rumors and Reports)
Other Threads in the Perl Forum
- Previous Thread: Parsing Problem
- Next Thread: URGENT!!!How to suppress Pop ups in Perl?
Views: 3486 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Perl





