Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Bit of a silly question, I'm afraid - but it really does bug me...

If a variable is required in an if statement:

if ($var) { #do this... } else { #do that }

Say '#do this' is turn all text red, but then I have no operation for '#do that' except 'get on with the rest of the script'.

Should I not use an else, or should I use something like (but not exactly) 'last;' to indicate that the block should be exited and the script proceed with?

Thanks for clearing this up for me.

Replies are listed 'Best First'.
Re: empty 'else' block
by Zaxo (Archbishop) on Nov 20, 2004 at 07:07 UTC

    If you write that as, do {this} if $var; then there is no poddibility of an else block.

    By skipping an expression when a condition is not met, you can avoid lots of difficult chains of logic.

    After Compline,
    Zaxo

Re: empty 'else' block
by davido (Cardinal) on Nov 20, 2004 at 06:50 UTC

    I guess I'm missing the point. You don't have to have an else segment.

    If you want your if() to terminate after executing the truth segment, but you don't want to wrap the entire rest of your script inside of an else segment, you can die or exit at the end of the truth segment, and forgo using an else segment. If you want it to execute the truth segment, and then execute the rest of the script regardless of the outcome of the if() evaluation, then just don't use an 'else' segment.

    Only use an else segment if you want a mutually exclusive logical branch in the program's execution.


    Dave

Re: empty 'else' block
by pg (Canon) on Nov 20, 2004 at 06:44 UTC

    It is a bad idea to put a last there.

    1. If this if statement is inside a loop, you will go out of the loop, which is not what you want;
    2. If this is not inside a loop, Perl does not like it;

    Actually I sometime intentionally add blank else blocks in my code, simply to tell myself that, "I DID think about whether anything need to be done in the else branch, but really nothing need to be done here, I didn't foget".

    To do that, simply leave it blank, or put a ; there.

    use strict; use warnings; my $var; if ($var) { print $var; } else { ; } print "Hello World!";

      I'm not usually a fan of putting semantics in comments, but here I think I might make an exception for:

      if(&predicate(...)) { &foo(...); } else { # do nothing }

      It's a bit more explicit to some poor maintenance programmer than a lone, apparently useless, semi. Of course, if lone, apparently useless, semis are a coding convention for "do nothing" where you hack, it's okay. :-)

      --
      Yours in pedantry,
      F o x t r o t U n i f o r m

      "Anything you put in comments is not tested and easily goes out of date." -- tye

Re: empty 'else' block
by nedals (Deacon) on Nov 20, 2004 at 07:03 UTC
    Or put another way...
    if ($var) { #do this... } #get on with the rest of the script
Re: empty 'else' block
by dws (Chancellor) on Nov 20, 2004 at 16:11 UTC

    Among the decent answers above, FoxtrotUniform makes a point that's worth expanding on a bit.

    Someday in the future, someone (perhaps you) might pick up the code to extend it, and wonder at the logic. The conditional logic you've shown is simple, but conditional logic can get awkward. Leaving an empty else (or elsif) block is a way to signal to the future reader that you've considered that branch of the logic tree, and that there's nothing to do. If the logic is painfully simple, leaving a blank note isn't necessary, an intrudes on readability. But if the logic is messy, adding a short explanatory comment to the empty can save some future reader a lot of puzzling and pondering. It's a matter of finding the right balance.

Re: empty 'else' block
by logan (Curate) on Nov 20, 2004 at 19:57 UTC
    Actually, I have stuff like this littered throughout my code. When I'm developing, I need something to happen to tell me the if statement didn't execute. If the statement is something like this:
    if ($foo) { print LOG "Exit condition $foo reached.\n"; exit 0; }
    ...then no big deal. If the code is more like this:
    if ($line =~ /\w+sql=\w+/i) { $db = "mysql"; $db_loc = (split(/=/, $line))[1]; }
    ...then during the testing period I need to account for a failure condition.
    else { print DEBUG "Error: Couldn't parse a database out of line ($line)\n +"; }
    In the field, I'd probably delete the error message, but there's a good chance that I'd leave the else statement in so I could re-enable it later if neccessary.

    -Logan
    "What do I want? I'm an American. I want more."

Re: empty 'else' block
by TedPride (Priest) on Nov 20, 2004 at 13:19 UTC
    ++Zaxo. There's no need to use else unless you need it - it just adds to the length of your code. I often do something like:
    print "Items " . join(', ', @items) . " have been found.\n" if $#items + != -1;
      Completely offtopic, but that can be better written as:
      print "Items " . join(', ', @items) . " have been found.\n" if @items;

      It's a little easier to read, imho.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: empty 'else' block
by mchiles (Initiate) on Nov 21, 2004 at 00:50 UTC
    You don't need that "else", so be kind to your keyboard and save a { and a }. The default "else" is do nothing anyway, which is what you want to do if an if isn't. -Matt :)
Re: empty 'else' block
by DaWolf (Curate) on Nov 21, 2004 at 16:17 UTC
    As you can see, it's just a matter of personal taste :)

    I personally like logan's answer. I often do that to debug my code and it's a pretty useful and easy way to find if your statement is valid.

    BTW, I love "silly" questions like this, because altough it's a simple question, it generates excellent opinions on basic logic, wich is always a good thing. :)

    Regards,