NAME
Loop::Util - loop helper keywords
SYNOPSIS
use Loop::Util;
my @array = qw(foo bar baz quux);
for my $item (@array) {
iffirst {
print "Items\n";
print "-" x length($item), "\n";
}
print "$item\n";
iflast {
print "-" x length($item), "\n";
print "Count: ", scalar(@array), "\n";
}
}
DESCRIPTION
This module adds statement keywords:
loop BLOCK,loop (EXPR) BLOCKloopintroduces new loop forms.loop { ... } loop(3) { ... } loop(get_number()) { ... }loopalso supports a single-statement form:loop process_input(); loop(3) process_input();Parentheses are required around the loop count expression. If no count is given, the loop is infinite, but
lastcan be used to jump out of the loop. (Thenextandredokeywords also work as expected.)iffirst [LABEL] BLOCK [else BLOCK]Runs
BLOCKonly on the first loop iteration; if anelseblock is present it runs for subsequent iterations.When
LABELis present,iffirstchecks that labeled loop context instead of the innermost loop. This is useful in nested loops:OUTER: loop(2) { loop(3) { iffirst OUTER { say "hi" } } }iffirstworks inloop{}loops, and also infor/foreachloops over arrays and lists. Callingiffirstin other loop kinds throws a runtime error.iflast [LABEL] BLOCK [else BLOCK]Runs
BLOCKonly on the last loop iteration; if anelseblock is present it runs for not-last iterations.When
LABELis present,iflastchecks that labeled loop context instead of the innermost loop.iflastworks in finiteloop{}loops, and also infor/foreachloops over arrays and lists. Callingiflastin other loop kinds throws a runtime error.ifodd [LABEL] BLOCK [else BLOCK]Runs
BLOCKfor odd-numbered iterations (1st, 3rd, 5th...). If anelseblock is present, it runs on even-numbered iterations.Note that if you loop through an array, the first iteration (an odd iteration) has index number 0 (an even number).
When
LABELis present,ifoddchecks that labeled loop context instead of the innermost loop.ifoddworks inloop{}loops, and also infor/foreachloops over arrays and lists. Callingifoddin other loop kinds throws a runtime error.ifeven [LABEL] BLOCK [else BLOCK]Runs
BLOCKfor even-numbered iterations (2nd, 4th, 6th...). If anelseblock is present, it runs on odd-numbered iterations.Note that if you loop through an array, the first iteration (an odd iteration) has index number 0 (an even number).
When
LABELis present,ifevenchecks that labeled loop context instead of the innermost loop.ifevenworks inloop{}loops, and also infor/foreachloops over arrays and lists. Callingifevenin other loop kinds throws a runtime error.__IX__Psuedo-constant that returns the current zero-based index of the loop.
It should work for
loop{}loops as well asfor/foreachloops over arrays or lists. In other contexts where no index can be determined, it returnsundef.
BUGS
Please report any bugs to https://github.com/tobyink/p5-loop-util/issues.
SEE ALSO
Acme::Loopy, Syntax::Keyword::Loop.
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT AND LICENCE
This software is copyright (c) 2026 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.