develooper Front page | perl.perl5.porters | Postings from October 2007

Re: [perl #46579] docpatch for C<last>

David Nicol
October 26, 2007 13:24
Re: [perl #46579] docpatch for C<last>
Message ID:
On 10/20/07, Steven Schubiger <> wrote:
> Ron Koerner <> wrote:
> ``last cannot be used to exit a block which returns a value such as eval {} ,
> sub {}  or do {} , and should not be used to exit a grep() or map() operation."
> > The documentation suggests that a thing like the above is not possible or
> > forbidden or breaks out of the if-block, but it does not give a hint to
> > the current behaviour.
> I don't think behaviors which shouldn't be used (and could be subject to change)
> should be documented at all.

I think the word "should" means one thing in documentation, and that one
thing is "rewrite me please!"

Here's the last entry in perlfunc rewritten in inverted pyramid style with
the addition of alluding to the similarity to "die" and the "cannot do this"
bits refactored into instructions for how to do the thing for which one
might be trying to use C<last>.

$ diff -u perl-5.9.4/pod/perlfunc.pod  perl-5.9.4.modified/pod/perlfunc.pod
--- perl-5.9.4/pod/perlfunc.pod 2006-08-15 07:37:42.000 -0500
+++ perl-5.9.4.modified/pod/perlfunc.pod        2007-10-26 15:13:52.000 -0500
@@ -2594,27 +2594,37 @@

 =item last

-The C<last> command is like the C<break> statement in C (as used in
-loops); it immediately exits the loop in question.  If the LABEL is
-omitted, the command refers to the innermost enclosing loop.  The
-C<continue> block, if any, is not executed:
+The C<last> command immediately exits a C<for>, C<while> or
+C<until> loop or a simple lexical block. It is much like the
+C<break> statement in C (as used in loops).  If the LABEL
+is omitted, the command refers to the innermost enclosing
+eligible block at run time. If the LABEL is not found or last
+appears without a label outside of any last-exitable blocks
+on the call stack, a trappable exception is thrown.
+The C<continue> block, if any, is not executed.

     LINE: while (<STDIN>) {
        last LINE if /^$/;      # exit when done with header

-C<last> cannot be used to exit a block which returns a value such as
-C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit
-a grep() or map() operation.
+Use of last to escape from a grep() or map() operation is
+poor style and may cause memory leaks as well as being very
+To return a value from a C<sub> or C<eval {}> use C<return>.
+To jump to the end of a C<do {}> block use C<goto LABEL>.

-Note that a block by itself is semantically identical to a loop
-that executes once.  Thus C<last> can be used to effect an early
-exit out of such a block.

 See also L</continue> for an illustration of how C<last>, C<next>, and
 C<redo> work.

+See also L</die> and L</eval> for a more general-purpose
+mechanism of abandoning what you are currently doing and
+resuming somewhere up the call stack.
 =item lc EXPR
 X<lc> X<lowercase> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About