Programming Basics

  Home arrow Programming Basics arrow Expression Modifiers for Perl Control ...
PROGRAMMING BASICS

Expression Modifiers for Perl Control Flow Constructs
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-03-07

    Table of Contents:
  • Expression Modifiers for Perl Control Flow Constructs
  • Looping Constructs

  •  
     

    SEARCH CODEWALKERS

    Expression Modifiers for Perl Control Flow Constructs


    (Page 1 of 2 )

    In this fifth part of a nine-part article series on Perl control structures, you'll learn about expression modifiers, looping statements, and more. This article is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    The unless Statement

    There’s another way of saying if (not $a). As always in Perl, there’s more than one way to do it.2 Some people prefer to think “If this is not true, then{ ... },” but other people think “Unless this is true, then{ ... }. ” Perl caters to both sets of thought patterns, and we could just as easily have written this:

    unless ($a) {
    print "\$a is not true\n";
    }

    The psychology is different, but the effect is the same. We’ll see later how Perl provides a few alternatives for these control structures to help them more effectively fit the way you think.

    Expression Modifiers

    When we’re talking in English, it’s quite normal for us to say

    1. If this is not true, then this happens, or
    2. Unless this is true, this happens.

    Similarly, it’s also quite natural to reverse the two phrases, saying

    1. This happens if this is not true, or
    2. This happens unless this is true.

    In Perl-speak, we can take thisifstatement:

    if ($number == 0) {
    die "can't divide by 0";
    }

    and rewrite it using expression modifiers as follows:

    die "can't divide by 0" if $number == 0;

    Notice how the syntax here is slightly different, it’s action ifcondition. There is no need for parentheses around the condition, and there are no curly braces around the action. Indeed, the indentation isn’t part of the syntax, so we could even put the whole statement on one line. Only a single statement will be covered by the condition. This form of theifstatement is called an expression modifier.

    We can turnunlessinto an expression modifier too, so, instead of this:

    if (not $name) {
    die "\$name has a false value";
    }

    you may find it more natural to write this:

    die "\$name has a false value" unless $name;

    Using Short-Circuited Evaluation

    There is yet another way to do something if a condition is true. By using the fact that Perl stops processing a logical operator when it knows the answer, we can create a sort of unlessconditional:

    $name or die "\$name has a false value";

    How does this work? Well, it’s reliant on the fact that Perl uses short-circuited, or lazy, evaluation to give a logical operator its value. If we have the statementX or Y, then ifXis true, it doesn’t matter whatY is, so Perl doesn’t look at it. IfXisn’t true, Perl has to look atYto see whether or not that’s true. So if$namehas a true value, then thedie()function will not be executed. Instead, it will do nothing and continue on executing the next statement.

    This form of conditional is most often used when checking that something we did succeeded or returned a true value. We will see it often when we’re handling files.

    To create a positiveifconditional this way, useand instead ofor. For example, to add one to a counter if a test is successful, you may say

    $success and $counter++;

    If you’ll recall,andstatements are reliant on both substatements being true. So, if$successis not true, Perl won’t bother evaluating$counter++and upping its value by 1. If$successwas true, then it would.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap