Cycle for in Perl

In the Perl programming language there are several ways to create a loop. One of the ways is to use the keyword for.

Here's an example:

▶ Run
#!/usr/bin/perl

for (my $i=0; $i < 3; $i++) {
    print "$i\n";
}

The program displays three lines with numbers:

0
1
2

How does the cycle for

Cycle for in the Perl programming language is very similar to the cycle for language C programming.

After for in the brackets 3 is a code snippet:

  • the initial value
  • check
  • change

First, run the first code snippet. Then check for a true or false in the second code snippet. If the validation returns true it executes the loop body if the test returns false, then the loop terminates. If you ran the body of the loop, after executing code in the loop body code is executed in the third piece, then a check is made again and everything goes on according to the already described algorithm.

It's possible the situation the body of the loop will not be executed even once. This happens if the first check returns false.

The lack of values

An interesting feature of the cycle is that any code snippet in the description of the cycle you may not be.

Here is an example of a cycle which is not set the initial value:

▶ Run
#!/usr/bin/perl

my $i = 0;

for (; $i < 3; $i++) {
    print "$i\n";
}

In the case if the description of the cycle for test is not available, it creates an infinite loop. Here is an example of the cycle for which doesn't specify validation, but still this cycle ended migrated in the body of the loop and is used last.

▶ Run
#!/usr/bin/perl

for (my $i=0;; $i++) {
    last unless $i < 3;
    print "$i\n";
}

In the description of the cycle of the third fragment also may be missing:

▶ Run
#!/usr/bin/perl

for (my $i=0; $i < 3;) {
    print "$i\n";
    $i++;
}

If in a loop to delete all the fragments, it will create an infinite loop:

#!/usr/bin/perl

for (;;) {
    print "infinity\n";
}

The visibility of a variable

In that case if the variable is defined in the description of the loop outside the loop, this variable will not be available. In case the code is use strict;, then such an appeal will fail.

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

for (my $i=0; $i < 3; $i++) {
    print "$i\n";
}

print $i;
Global symbol "$i" requires explicit package name (did you forget to declare "my $i"?) at script.pl line 10.
Execution of script.pl aborted due to compilation errors.

If you need to use a variable counter outside of the loop body, you have to find it out description of the cycle:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;

for (; $i < 3; $i++) {
    print "$i\n";
}

print $i;

next and last

In Perl there are special keywords which you can use to influence the execution of the loop.

Keyword next stops executing the current iteration:

▶ Run
#!/usr/bin/perl

for (my $i=0; $i < 3; $i++) {
    next if $i == 0;
    print "$i\n";
}

And with the help of last at any time to complete the cycle:

▶ Run
#!/usr/bin/perl

for (my $i=0; $i < 3; $i++) {
    last if $i == 1;
    print "$i\n";
}

Related topics

Other articles