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

This morning while trying to answer an SOPW I wanted to give a citatation explaining how to use if...elsif...else. Naturally, I thought of perlsyn, but when I looked there all I could find was the somewhat cryptic if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK. There was no discussion or comment. (For a direct link to this,see the "here" in BrowserUk's post below).

The only way I found even that was my handy-dandy firefox search button. Conditionals have no section of their own.

It is common in discussions of computer language syntax to have a whole section on conditions. perlsyn has one on loops, but not on conditionals. There doesn't seem to be a separate perldoc on conditionals and the conditional section in perlop only covers  expr ? expr : expr syntax.

Is there a reason for this? Is this an oversight? An oversight seems unlikely given the maturity of this documentation and the importance of the syntax. Another place to look?

Thanks in advance, beth

Update: there appears to be a brief example of "if...elsif..else" in perlintro - see post by Anonymous below.

Replies are listed 'Best First'.
Re: Where is if...elsif...else documented in perldoc?
by BrowserUk (Patriarch) on Jun 26, 2009 at 04:28 UTC

    Here?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Where is if...elsif...else documented in perldoc?
by graff (Chancellor) on Jun 26, 2009 at 04:39 UTC
    I'm looking at a 5.8.8 man page for perlsyn, and I wonder what you mean by saying that there is "no discussion or comment" about if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK -- what's wrong with this:

    Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not statements. This means that the curly brackets are required−−no dangling statements allowed. If you want to write conditionals without curly brackets there are several other ways to do it. The following all do the same thing: ...

    The "if" statement is straightforward. Because BLOCKs are always bounded by curly brackets, there is never any ambiguity about which "if" an "else" goes with. If you use "unless" in place of "if", the sense of the test is reversed.

    What more do you think needs to be said (apart from stating the obvious)?

    Maybe it's a bit "off the beaten track" for this information to be in a section titled "Compound Statements", which begins with a definition of a BLOCK that is based on "scope", and an overview of what establishes a BLOCK. But it seems appropriate enough in terms of clarifying how conditional statements work in perl.

      An example might be nice!

      The example that follows the section you cited uses neither else nor elsif. In fact there are no examples using either else or elsif in the document. As for the discussion, it addresses a language design issue (ambiguity) not a usage issue.

      Only the most sophisticated readers can comfortably and confidently translate syntax synopsi into working code. That is not something I really felt comfortable doing until I had at least reading knowlege of about 6 different computer languages and studied compilers. I admit that others are probably quicker on the draw than I am, but I think converting syntax descriptions into code without the help of examples is a bit much to ask your average Perl learner.

      Unless we are saying that perldoc is only meant for CS majors and self-taught equivalents. Considering that perldoc contains things like perlreftut and perlretut and perltoot, I would hope that perldoc is trying to go beyond internal language discussions.

      To quote something BrowserUk said a few months ago when we were discussing new monkquips:

      Don't forget you've probably already forgotten, how hard it was to learn what you now take for granted.

      The point of this place is not: to force newbies to go through the same pain you had to go through.

      Re: I want more monkquips

      Best, beth

      Update: Many thanks to anonymous below - wish I could upvote a real person. However, having the example far removed from the main place to look for documentation of syntax seems not as helpful as it could be. I didn't consider even looking there because I generally consider that too high level to be of much help. Live and learn.

        To quote something BrowserUk said a few months ago ... The point of this place ...

        Don't take this the wrong way--I actually agree with you that the Perldocs could do with (links) to more examples--but there is a distinct and extremely important difference between this place and the Perldocs.

        I'm not sure if it is a limitation of POD; the MAN format; or simply tuits; but the biggest limitation of the Perldocs is not content, but structure.

        Given the hierarchal nature of HTML, it would be really nice to utilise that to have a keyword index; that linked keywords to summary descriptions; that linked to basic examples; that linked to expanded discussion; that linked to error messages and common pitfalls.

        Probably the best producers of program and language documentation (that I've encountered), were the long defunct guys and gals at DEC. Besides that they appreciated the difference between understanding a subject and being able to convey it; their greatest skill was knowing that sometimes removing words is better than adding them.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        NO! In the last few years, the reference perldocs have been littered with more and more examples and tutorials. Don't. I like references. Perl comes with tutorial perldocs too, if there has to be an example, put it there. I hate the form the core perldocs have taken these days.