views:

1196

answers:

9

I am new to Perl. Does anyone know why Perl has 'elsif' and not 'elseif'?

I am doing something like this:

$somevariable = 0;
if ($idcount==5) 
{
   //do something
   if (somestatement is true) // 1 
   {
      $somevariable = 1;
   }
}
elsif ($idcount > 1 && somevariable = 0)
{
    //do something else here
}

The code never comes to the elsif statement. In this case idcount is actually 5, so it comes to the first if statement. But an inner if statement (1) is not true. so $somevariable is still 0. So since idcount is 5, it should come to elsif as well since $idcount is greater than 1 and $somevariable is still 0.

Maybe I am just tired and not seeing something obvious.

+10  A: 

No, anything in an elsif only has a chance to be executed if none of the previous if/elsif conditions have been true, that's the "else" part of it.

If you want both to be evaluated, just make it an if instead of an elsif.

Chad Birch
+4  A: 

I don't see what your confusion has to do with the fact that perl uses "elsif" rather than "else if" or "elseif". In every language I've ever used, from pre-1970s-style FORTRAN to Java and including along the way such "highlights" as Easytreive, if you do "if cond ... else ..." the stuff after the else doesn't get executed if the original "cond" is true. That's what "else" means.

In your case, the "cond" is "($idcount==5)", which you already stated is true, and the else part that doesn't get executed because it's true is "if ($idcount > 1 && $somevariable == 0) ...".

Paul Tomblin
+2  A: 

"elsif" in Perl works exactly the same as "elseif", "else if", and "elif" in other languages. It's just a matter of spelling.

Barry Brown
+1  A: 

If you're going to use if...elsif in any language, your conditions should be mutually exclusive. The two conditions in your example could both be true simultaneously, so you should break that up into two separate if statements.

Bill the Lizard
+7  A: 
elsif ($idcount > 1 && somevariable = 0)

Should be:

elsif ($idcount > 1 && $somevariable == 0)

The assignment operator returns the assigned value, 0, which equates to false, thus the logical and '&&' always fails.

spoulson
Good point, but it doesn't fix the OP's issue.
Perchik
Sure it does. The bug prevents "elsif" from reaching that code block. The rest of the code looks ideal.
spoulson
No, it's a separate problem. styris was expecting the code to go into the if-block and then come out of there and go into the elsif-block.
Chad Birch
Yeah, you're right. I wasn't reading carefully enough.
spoulson
Don't forget the $ in front of somevariable
Barry Brown
+6  A: 

Here is how the Llama book answers the elsif vs elseif question:

"Because Larry says so."

Chuck Phillips
+1  A: 

Here is the equivalent of what was posted in the question.

use strict;
use warnings;

$somevariable = 0;
if ($idcount==5) 
{
  # do something
  if ( 1 )
  {
    $somevariable = 1;
  }
}
else
{
  if ($idcount > 1 && somevariable == 0) # this line was wrong in the question
  {
    # do something else here
  }
}

or

use Modern::Perl;

$somevariable = 0;
given($idcount){
  when( 5 ){
    # do something
    if ( 1 ){
      $somevariable = 1;
    }
  }
  when( $_ > 1 && $somevariable == 0 ){
    # do something else here
  }
}

use Modern::Perl; is the same as use strict; use warnings; use 5.010;

Brad Gilbert
+3  A: 

In any if-elsif-else construct, you only ever execute the first branch whose condition is true. Once that branch executes, the program picks up after the entire if-elsif-else.

If you want to do something with possible multiple branches, you want the given-when feature in Perl 5.10. I explain in the the latest edition of Learning Perl, but there are plenty of examples online too.

As for the spelling of elsif, there are a couple of factors involved.

In C, you write

if( ... ) {
   ... }
else if ( ... ) {

That sometimes causes a "dangling else" problem. If you had started but didn't finish inserting an if-else if right before another, but distinct, if, you've combined the two through the dangling else. Larry Wall didn't like that, so he combined the 'else if" into one word, "elseif". However, that looked odd to Larry since the rule is "i before e except after c, ...", so he fixed that by taking out the "e" to make it just "elsif".

brian d foy
+1  A: 

Perl has elsif because perl doesn't allow C style "else if" without brackets. Instead, you would need to have something like:

if (x) {

}
else {
  if(y) {

  }
}
Plasmer