Perl last Function



This is not a function. The last keyword is a loop-control statement that immediately causes the current iteration of a loop to become the last. No further statements are executed, and the loop ends. If LABEL is specified, then it drops out of the loop identified by LABEL instead of the currently enclosing loop.


Following is the simple syntax for this function −

last LABEL


Return Value

This does not return any value.


Following is the example code showing its basic usage −


$count = 0;

while( 1 ){
   $count = $count + 1;
   if( $count > 4 ){
       print "Going to exist out of the loop\n";
       print "Count is $count\n";
print "Out of the loop\n";

When above code is executed, it produces the following result −

Count is 1
Count is 2
Count is 3
Count is 4
Going to exist out of the loop
Out of the loop