Is there any advantage to using a continue block with a while loop in Perl?

SYNTAXIS 1:

while { #some code } continue { #some other code } 

SYNTAXIS 2:

 while { #some code #some other code } 

Does SYNTAX 1 have an advantage over SYNTAX 2? assuming that "some code" and "some other code" remain the same set of lines in both syntaxes. Or its just two different styles that have no advantage in coding.

+5
source share
1 answer

The continue block executes when you call next from the middle of the loop, so it provides a way to execute some common code between iterations, regardless of the execution path through each iteration.

Comparison

 my $last_item; for my $item (@list) { if ($last_item eq $item) { do_something(); $last_item = $item; next; } if (condition2($item,$last_item)) { $last_item = $item; next; } do_something_else(); $last_item = $item; } 

with

 my $last_item; for my $item (@list) { if ($last_item eq $item) { do_something(); next; } if (condition2($item,$last_item)) { next; } do_something_else(); } continue { $last_item = $item; } 

Some examples of continue in the wild:

HTTP::Cookies

PPIx::Regexp::Node

PDL::Core

+6
source

Source: https://habr.com/ru/post/1272196/


All Articles