Summary

Conditionals and loops are the switches and dials of your Perl script. Without them, you can get stuff done, but it'll be the same stuff each time and your script will turn out pretty dull. Conditionals and loops allow you to make decisions and change what your script does based on different input or different situations. Conditional statements are those that branch to different blocks of Perl code depending on whether a test is true or false. You learned about the if, the ifelse, and the ifelsif constructs for building conditionals, as well as the conditional operator ?..:, which can be nested in other expressions, and the use of the logical operators (&&, ||, and and or) as conditionals.

Next, we moved onto loops: specifically the while loops, which repeat a block of statements an unidentified number of times based on a test. There are four loops we covered here: while, until, dowhile, and dountil.

The second kind of loop is the for loop, which also repeats a block of statements, but emphasizes the number of times aspect of the loop much more. You learned about the foreach loop for working through elements in a list, and the more general-purpose for loop.

You then learned about loop controls, which allow you to stop executing a block and skip to some part of a loop: next, last, and redo, as well as using labels to control jumping around inside nested loops.

And finally, we ended this lesson as we did the previous two: with more notes about input, including using the <> syntax to read from files and using the $_ variable as a shortcut in many popular operations.

After this lesson, you've learned the core of the Perl language, and we'll explore some longer examples tomorrow to finish up the week. But don't stop now—next week's lessons will introduce you to some of the most powerful and exciting features of Perl, including various ways of processing lists and pattern matching.

The functions you learned about in this lesson include

  • do, a function that behaves like loop with a while or until at the end of it.

  • rand generates a random number between 0 and its argument

  • srand seeds the random number generator used by rand. Without an argument, srand uses the time as the seed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset