while ( ) { next if $_ eq 'foo'; # Where foo can only appear once in the file print; } #### #!/usr/bin/perl use strict; use warnings; do_something(5); sub do_something { my $target = shift; my $block; my $block2 = sub { print "No need to check $_ against $target anymore\n"; }; my $block1 = sub { if ( $_ == $target ) { print "Skipping $target\n"; $block = $block2; } else { print "$_ is not $target\n"; } }; $block = $block1; for ( 1 .. 9 ) { $block->(); } }