Some parts of this page were machine translated.
Powered by Yandex.Translate
http://translate.yandex.com/
In the Perl programming language there are several ways to create a loop.
One of the ways is to use the keyword for
.
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:
#!/usr/bin/perl
for (my $i=0; $i < 3; $i++) {
print "$i\n";
}
The program displays three lines with numbers:
0
1
2
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:
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.
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:
#!/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
.
#!/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:
#!/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";
}
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.
#!/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:
#!/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:
#!/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:
#!/usr/bin/perl
for (my $i=0; $i < 3; $i++) {
last if $i == 1;
print "$i\n";
}