Programming Basics

  Home arrow Programming Basics arrow Perl Operators and Control Flow Constr...
PROGRAMMING BASICS

Perl Operators and Control Flow Constructs
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2012-02-15

    Table of Contents:
  • Perl Operators and Control Flow Constructs
  • Comparing Numbers

  •  
     

    SEARCH CODEWALKERS

    Perl Operators and Control Flow Constructs


    (Page 1 of 2 )

    In this second part to a nine-part series on Perl control structures, we'll delve more deeply into operators and take a look at how to compare numbers. It is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    Operators Revisited

    The ifstatement, and all the other control structures we’re going to visit in this chapter, test to see if a condition is true or false. They do this using the Boolean logic mentioned in Chapter 2, together with Perl’s ideas of true and false. To remind you of these:

    1. An empty string,"", is false.
    2. The number 0 and the string"0"are both false.
    3. An empty list,(), is false.
    4. The undefined value is false.
    5. Everything else is true.

    However, you need to be careful for a few traps here. A string containing invisible characters, like spaces or newlines, is true. A string that isn’t"0"is true, even if its numerical value is 0, so"0.0"for instance, is true.

    Larry Wall has said that programming Perl is an empirical science—you learn things about it by trying them out. Is(())a true value? You can look it up in books and the online documentation, or you can spend a few seconds writing a program like this:

    #!/usr/bin/perl -w
    # emptylist.pl

    use strict;

    if ( (()) ) {
    print "Yes, it is.\n";
    }

    This way you get the answer straight away, with the minimum of fuss. (If you’re interested, it isn’t a true value.) We’ve also seen that conditional operators can test things out, returning1if the test was successful and empty string if it was not. Let’s see more of the things we can test.

    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