
Laurent_R
Veteran
/ Moderator
Aug 24, 2014, 4:33 AM
Post #3 of 4
(3518 views)
|
Re: [PerlMonkey] Labelled Break Statement Query
[In reply to]
|
Can't Post
|
|
In the specific case you've shown, not only are the braces useless (but, as pointed out by Fishmonger, they do no harm), but even the "READ_NEW_FILE:" label is useless, as a simple "last;" statement would suffice. Labels are really useful when you have nested loops and want to exit from several loop nesting levels in one go. This is an brief (and perhaps somewhat clumsy) example in which using a label really makes sense (although the code below could probably be written differently, such as removing comments before splitting the line):
READ_NEXT_LINE: while (my $line = <$file>) { my @words = split /\s+/, $line; for my $item (@words) next READ_NEXT_LINE if $item =~ /^#/ ; # skip the rest of @words if it is comments # ... further processing of $item } } Here, if one of the $items start with '#', the rest of the line is a comment and we just want to skip the rest of the items and pick up the next input line.
|