Skip to Content
Learning Perl, Fourth Edition
book

Learning Perl, Fourth Edition

by Randal L. Schwartz, Tom Phoenix, brian d foy
July 2005
Beginner
312 pages
9h 23m
English
O'Reilly Media, Inc.
Content preview from Learning Perl, Fourth Edition

The elsif Clause

Every so often, you may need to check a number of conditional expressions, one after another, to see which one of them is true. This can be done with the if control structure’s elsif clause, as in this example:

    if ( ! defined $dino) {
      print "The value is undef.\n";
    } elsif ($dino =~ /^-?\d+\.?$/) {
      print "The value is an integer.\n";
    } elsif ($dino =~ /^-?\d*\.\d+$/) {
      print "The value is a _simple_ floating-point number.\n";
    } elsif ($dino eq '') {
      print "The value is the empty string.\n";
    } else {
      print "The value is the string '$dino'.\n";
    }

Perl will test the conditional expressions one after another. When one succeeds, the corresponding block of code is executed, the whole control structure is done,[238] and the execution goes on to the rest of the program. If none has succeeded, the else block at the end is executed. (The else clause is optional, though in this case it’s often a good idea to include it.)

There’s no limit to the number of elsif clauses, but Perl has to evaluate the first 99 tests before it can get to the hundredth. If you’ll have more than half a dozen elsifs, you should consider whether there’s a more efficient way to write it. The Perl FAQ (see the perlfaq manpage) has a number of suggestions for emulating the “case” or “switch” statements of other languages.

You may have noticed by this point that the keyword is spelled elsif, with one e. If you write it as “elseif,” with a second e, Perl will tell you that it is the incorrect spelling. Why? ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 3rd Edition

Learning Perl, 3rd Edition

Tom Phoenix, Randal L. Schwartz
Learning Perl, 6th Edition

Learning Perl, 6th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Mastering Perl

Mastering Perl

brian d foy

Publisher Resources

ISBN: 0596101058Catalog PageErrata