next skips to the next item, and last stops the loop early.

Foreach with Next and Last

foreach_next_last.pl
use strict;
use warnings;

my $stop = ;
my @values = (1, 2, 3, 4, 5, 6);
my $kept = 0;
my $seen = 0;

foreach my $value (@values) {
    if ($value > $stop) {
        last;
    }
    if ($value % 2 == 0) {
        next;
    }
    $kept = $kept + $value;
    $seen = $seen + 1;
}

print "stop=$stop\n";
print "seen=$seen\n";
print "kept=$kept\n";
use strict;
use warnings;

my $stop = ;
my @values = (1, 2, 3, 4, 5, 6);
my $kept = 0;
my $seen = 0;

foreach my $value (@values) {
    if ($value > $stop) {
        last;
    }
    if ($value % 2 == 0) {
        next;
    }
    $kept = $kept + $value;
    $seen = $seen + 1;
}

print "stop=$stop\n";
print "seen=$seen\n";
print "kept=$kept\n";
use strict;
use warnings;

my $stop = ;
my @values = (1, 2, 3, 4, 5, 6);
my $kept = 0;
my $seen = 0;

foreach my $value (@values) {
    if ($value > $stop) {
        last;
    }
    if ($value % 2 == 0) {
        next;
    }
    $kept = $kept + $value;
    $seen = $seen + 1;
}

print "stop=$stop\n";
print "seen=$seen\n";
print "kept=$kept\n";
loop control Loop control statements change how the current loop continues.