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

#!/usr/bin/perl -w use strict; use warnings; my$str = 'perl_script'; my$length = length $str; if ($str eq 'perl_script') { print 'both strings are same'; } elsif($length = 8){ print "the given string has 8 characters"; } elsif($length >8){ print "the given string has more that 8 characters"; } else{ print"Doen't matched both strings"; }
output: both strings are same. My question is why it doesn't executing elsif. What's the wrong in me code.I'm purely beginner please let us know

Replies are listed 'Best First'.
Re: Perl If and Elsif statement help
by kcott (Archbishop) on Dec 30, 2013 at 06:02 UTC

    G'day rammohan,

    I recommend you read "perlintro -- a brief introduction and overview of Perl". You'll find links to more detailed documentation throughout that page.

    The "Conditional and looping constructs" section will explain the syntax issues with your posted code.

    You also have a problem with the condition "($length = 8)". '=' is the assignment operator; here you want '==' which is the numerical comparison operator. These, and other operators, are described in the "Builtin operators and functions" section.

    I suspect the example code you were attempting should have looked more like this:

    #!/usr/bin/env perl -l use strict; use warnings; my $str = 'perl_script'; my $length = length $str; if ($str eq 'perl_script') { print 'Strings are the same.'; if ($length == 8) { print 'Length is exactly 8.'; } elsif ($length > 8) { print 'Length is more than 8.'; } else { print 'Length is less than 8.'; } } else { print 'Strings are different.'; }

    Output:

    Strings are the same. Length is more than 8.

    Also note that I haven't used the '-w' switch, but I have used the '-l' switch. Both of these are explained in "perlrun - how to execute the Perl interpreter".

    -- Ken

      Very nice neat and superb explanation..Thank you
Re: Perl If and Elsif statement help
by davido (Cardinal) on Dec 30, 2013 at 05:19 UTC

    Because that's how elsif works. If the first "if" evaluates true, its block is executed, and then the entire construct of remaining elsif and else statements is skipped. Execution resumes on the line following the closing brace of the last elsif or else.

    The elsif() condition is only evaluated for its Boolean result if the first if() was false.

    This is very much similar to how else if (...) works in C and C++. In fact, though there are many languages I don't know, I can't think of any that do it differently.


    Dave

Re: Perl If and Elsif statement help
by rammohan (Acolyte) on Dec 30, 2013 at 05:30 UTC
    Thank you for comment in my Question.Now i got some clarification on elsif block. Is there any online exercise for control statements in Perl.

      It's such a universal way of doing things, in that it applies to many, many languages, that many resources sort of gloss over it, with the exception of those that are geared to someone just learning to program, as opposed to a programmer learning Perl. You might read Wikipedia: Conditional (programming). Also, Learning Perl, 6th Edition (O'Reilly), the respected "Llama book" discusses elsif in chapter 10.


      Dave

      None that I know of. However Perl control structures are rather like most computer languages. See the perlsyn documentation for a description of what Perl provides.

      True laziness is hard work