Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, the last
keyword provides a mechanism to exit a loop prematurely. It allows you to break out of a loop without waiting for the loop condition to be false. It is similar to the break
statement in other programming languages.
last
in a basic loopHere's an example using a basic for
loop:
#!/usr/bin/perl use strict; use warnings; for (my $i = 0; $i <= 10; $i++) { if ($i == 5) { last; # Exits the loop when $i is 5 } print "$i\n"; } # This will print: # 0 # 1 # 2 # 3 # 4
In the above example, the loop is supposed to run from 0 to 10. However, due to the last
statement, the loop exits prematurely when $i
becomes 5.
last
in a while
loopHere's an example using a while
loop:
#!/usr/bin/perl use strict; use warnings; my $count = 0; while ($count < 10) { $count++; if ($count == 3) { last; # Exits the loop when $count is 3 } print "$count\n"; } # This will print: # 1 # 2
last
in a foreach
loopSimilarly, you can use last
with a foreach
loop:
#!/usr/bin/perl use strict; use warnings; my @fruits = qw(apple banana cherry date); foreach my $fruit (@fruits) { if ($fruit eq "cherry") { last; # Exits the loop when the fruit is cherry } print "$fruit\n"; } # This will print: # apple # banana
In nested loops, last
will only affect the innermost loop:
#!/usr/bin/perl use strict; use warnings; for my $i (1..3) { for my $j (1..3) { if ($j == 2) { last; # This will only break the inner loop } print "$i, $j\n"; } } # This will print: # 1, 1 # 2, 1 # 3, 1
In this nested loop example, the inner loop will exit when $j
is 2, but the outer loop will continue to iterate.
The last
keyword in Perl is a useful tool for controlling the flow of your loops. Whether you need to exit due to an error condition or some other criterion being met, last
provides a clean and straightforward way to do so.
Perl last statement in loop:
last
statement is used to exit a loop prematurely. It immediately terminates the loop's execution and moves to the next statement after the loop.foreach my $number (1..10) { last if $number > 5; print "$number "; }
Exiting a loop early with last in Perl:
last
can be used to exit a loop before it completes all iterations based on a certain condition.foreach my $item (@items) { last if condition($item); process_item($item); }
Using last to break out of a loop in Perl:
last
statement breaks out of the loop, regardless of the loop type (for, foreach, while, etc.).while ($condition) { last if another_condition(); # Loop body }
Perl last loop control flow:
last
immediately jumps out of the loop and continues with the next statement after the loop.foreach my $element (@array) { last if $element eq 'stop'; # Process element }
When to use last in Perl loops:
last
when you want to exit a loop prematurely based on a certain condition.foreach my $number (@numbers) { last if $number == 0; # Process non-zero numbers }
Breaking out of a nested loop with last in Perl:
last
can be used to break out of the innermost loop in nested loops.foreach my $outer (@outer_list) { foreach my $inner (@inner_list) { last if condition($inner); # Process inner } }
Perl last vs next in loops:
last
exits the loop, while next
skips the rest of the current iteration and moves to the next iteration.foreach my $element (@elements) { next if skip_condition($element); last if stop_condition($element); # Process element }
Conditional use of last in Perl:
last
conditionally based on a specific situation within the loop.foreach my $item (@items) { last if condition($item); # Process item }
Perl last statement examples:
last
is used to control loop flow.foreach my $name (@names) { last if $name eq 'stop'; print "$name "; }