943,905 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 4136
  • Perl RSS
Dec 16th, 2006
0

Empty block in if....else construct. OK?

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

perl Syntax (Toggle Plain Text)
  1. if ($beer == $wine) {# Some code}
  2. elsif ($beer == $vodka) {}
  3. 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.
Similar Threads
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Dec 16th, 2006
0

Re: Empty block in if....else construct. OK?

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:

Perl Syntax (Toggle Plain Text)
  1. # avoid autoloader warnings
  2. 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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Dec 16th, 2006
0

Re: Empty block in if....else construct. OK?

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.
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Dec 16th, 2006
0

Re: Empty block in if....else construct. OK?

Good luck with the project.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Dec 16th, 2006
0

Re: Empty block in if....else construct. OK?

Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Dec 31st, 2006
0

Re: Empty block in if....else construct. OK?

It is perfectly ok what compiler regards, but I agree it is not a good coding style.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nemo5 is offline Offline
6 posts
since Dec 2006
Jan 3rd, 2007
0

Re: Empty block in if....else construct. OK?

Hi,

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

Svet
Reputation Points: 18
Solved Threads: 6
Junior Poster in Training
lordspace is offline Offline
90 posts
since May 2006
Jan 3rd, 2007
0

Re: Empty block in if....else construct. OK?

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.
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Jan 12th, 2007
0

Re: Empty block in if....else construct. OK?

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.
Last edited by cscgal; Jan 12th, 2007 at 3:35 am. Reason: Fake signature snipped
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zabina is offline Offline
10 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Parsing Problem
Next Thread in Perl Forum Timeline: URGENT!!!How to suppress Pop ups in Perl?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC