1. Perl
  2. Syntax
  3. while statement

while statement - Repeat processing

You can write a iterative process using while statement. Repeat the contents of the block while the condition is true.

while (condition) {

Output numerical value from 0 to 2

This is an example that outputs a numerical value from 0 to 2 using while statement. The variable "$i" is incremented at the end of while statement. When "$i" becomes 3, the condition becomes false, so the while exits.

# Output numerical values from 0 to 2
my $i = 0;
while ($i <= 2) {
  print "$i\n";

What is the difference between while statement and for statement (or foreach statement)? ??

You can write all the iterative processing with while statement, but if you can write it with for statement or foreach statement, it is better to write it using for statement or foreach statement.

# for statement
for (my $i = 0; $i <= 2; $i++) {
  print "$i\n";

# foreach statement
foreach my $num (@nums) {
  print "$num\n";

As a side note, in Perl, for statement and foreach statement are exactly the same and can be rewritten to each other.

Example using infinite loop

A frequently used example of while is the description of an infinite loop. If you write 1 in the condition, the loop will repeat forever.

while (1) {

To break out of the loop, use last statement. Note that the loop escape is not break statement but last statement unlike C language.

In the example below, array elements are removed one by one with pop function. So, when there are no more elements in the array, it is judged by if statement, and the while block is exited by using last statement.

my @nums = (1, 2, 3);
while (1) {
  if (@nums) {
    pop @nums;
  else {

Read lines in order from standard input/command line arguments

As an example often used in while statement, use line input operator to order lines from standard input or a file given as a command line argument. Let's load it into.

# Read line
while (my $line = <>) {
  # Skip only the first line
  next if $. == 1;
  # Remove a line break
  chomp $line;
  # process
  print "Line: $line\n";

In this example, chomp function is used to remove a line break and output with "Line:".

Using predefined variable "$." and next statement, the line number is Processing is skipped only at the first line.

Use this program as follows. Using while statement and the line input operator, you can easily write a program that reads lines from a file or standard input.

# When reading a file name from a command line argument
perl script.pl file

# When reading from standard input using a pipe
cat file> perl script.pl

This can be multiple files.

# When reading a file name from a command line argument
perl script.pl file1 file2

# When reading from standard input using a pipe
cat file1 file2> perl script.pl

Related information

The iterative process is explained in detail below.

Perl also has do ~ while statement, but since the next and last statement cannot be used as they are, it is strongly recommended to use while statement. I recommend it.

Related Informatrion