my @a1 = 1 .. 10;
my @a2 = 1 ... 10;
print "@a1\na2\n";
The difference between them comes when you use the
operator in a scalar context. In this case the
operator returns false until the left operand is true. It
then continues to return true until the right operand
is true, at which point it returns false again (this is why
it is sometimes known as the flip-flop operator).
The difference between the two versions is that with two
dots the operator evaluates its left operand and if it is
true the operator evaluates the right operand immediately. It is therefore possible for the operator to switch from false to true and back to false on one evaluation. With
three dots, if the left operand is true, the operator
doesn't evaluate the right operand until the next
evaluation.
As an example, imagine you have a file and you are
interested in lines starting with the one containing 'BEGIN'
and ending with the one containing 'END'. You can write
code like this:
while (<FILE>) {
if (/BEGIN/ .. /END/) {
# do stuff
}
}
The difference is illustrated by the behaviour if you
have a line that contains both 'BEGIN' and 'END'. The code
as shown above will evaluate true (from 'BEGIN') and then
will immediately evaluate false (from 'END') and the nett
result is that it will return false. If you used the three
dot version of the operator, it would evaluate true and
evaluation would stop there. The right operand would not
be evaluated until the next line is processed.
Both of these behaviours can be useful, therefore both
operators exist :)
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
|