Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

The range operators are .. (two dots) and ... (three dots).

In list context both operators behave in the same way, returning a list of values counting from the left value to the right value (it even works on strings).

They're very useful for creating loops, slicing arrays, etc.

Example:

printf "Line %d\n", ++$i foreach ( 1 .. 10 );

In scalar context, they emulate the behaviour of the line range operator of sed.
To do this, the operator returns a boolean value, and this value is maintained across the duration of the routine that has the operator.

The operator returns false as long as its left range operand is false; once the left operand is true, the operator changes its state and returns true until the right operand is true; after the right operand is true the operator will return false again.

So the idea is to use them within a loop to do things such as extracting a range of lines.

The only difference between .. and ... is that the former test the right operand as soon as the left operand is true, while the latter waits for the next evaluation to check the value of the right operand.

Example:

#-- print all lines between <body> and </body>
while ( <> )
{
  print if ( /<body>/ .. <\/body> );
}