Skip to content

Instantly share code, notes, and snippets.

@isotopp
Created January 7, 2019 15:18
Show Gist options
  • Save isotopp/c4163ce9ab7eaf03699f072d64b812b0 to your computer and use it in GitHub Desktop.
Save isotopp/c4163ce9ab7eaf03699f072d64b812b0 to your computer and use it in GitHub Desktop.
Perl: Loop controls without loops
#! /usr/local/bin/perl --
#
# [kris:~] $ ./test.pl
# f1 start
# f2 start
# f3 start
# Exiting eval via next at ./test.pl line 26.
# Exiting subroutine via next at ./test.pl line 26.
# Exiting subroutine via next at ./test.pl line 26.
# f1 end
# end of code
use strict;
use warnings;
sub f1 {
print "f1 start\n";
# although this is not a loop. it seems equivalent to one.
# for or while loops will behave the same
{
f2();
}
print "f1 end\n"; # this line will execute as it's after the loop like closure
}
sub f2{
print "f2 start\n";
f3();
print "f2 end\n"; # this line will be skipped
}
sub f3 {
print "f3 start\n";
# eval doesn't stop it from going further up the call stack.
eval {
next; # loop-control statement not in a loop
1;
} or do {
print "f3 eval error\n";
};
print "f3 end\n"; # this line will be skipped
}
f1();
print "end of code\n";
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment