Chapter 6. Iteration
This chapter is about iteration, which is the ability to run a block of statements repeatedly. We saw a kind of iteration, using recursion, in “Recursion”. We saw another kind, using a for
loop, in “for Loops”. In this chapter we’ll see yet another kind, using a while
statement. But first I want to say a little more about variable assignment.
Assignment Versus Equality
Before going further, I want to address a common source of confusion. Because Perl uses the equals sign (=
) for assignment, it is tempting to interpret a statement like $a = $b
as a mathematical proposition of equality, that is, the claim that $a
and $b
are equal. But this interpretation is wrong.
First, equality is a symmetric relationship and assignment is not. For example, in mathematics, if then . But in Perl, the statement $a = 7
is legal and 7 = $a
is not.
Also, in mathematics, a proposition of equality is either true or false for all time. If now, then a will always equal b. In Perl, an assignment statement can make two variables equal, but they don’t have to stay that way:
> my $a = 5; 5 > my $b = $a; # $a and $b are now equal 5 > $a = 3; # $a and $b are no longer equal 3 > say $b; 5
The third ...
Get Think Perl 6 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.