Chapter 5

Control Flow

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • Working with if/elsif/else expressions
  • What are and when to use for/foreach loops
  • Understanding and working with while/until loops
  • Understanding the various statement modifiers and how to use do while/do until
  • What are given/when statements and statement modifiers and when to use them

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Perl.productCd-1118013847,descCd-DOWNLOAD.html on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Example_5_1_unique.pl
  • Example_5_2_arrays.pl

From previous chapters, you now understand some of the basics of Perl, but now you get closer to the heart of programming. When you program, you constantly make decisions loop over data based on those decisions. That’s what this chapter is all about: how Perl makes decisions and looping over data.

USING THE IF STATEMENT

This section starts with boolean logic. As explained in Chapter 4, the following values are considered “false” in Perl:

  • undef
  • "" (the empty string)
  • 0
  • 0.0
  • "0" (the “string” zero)

Some languages have specific boolean objects, or TRUE and FALSE identifiers. Perl does things a little differently. As you work through the examples, try to see what Perl does and why. If you have experience with programming languages that have a different approach, consider the strengths and weaknesses of the different ...

Get Beginning Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.