Jump to content

Perl Programming/Keywords/last

From Wikibooks, open books for an open world
Previous: kill Keywords Next: lc

The last keyword

[edit | edit source]

last is the same as break in C-like languages and is used to make the program immediately exit the current loop. Withou LABEL, the innermost loop is assumed. From Perl 5.18.0 onwards, it is possible to compute the LABEL during runtime by passing an EXPRESSION.

If a loop has a last command, the continue block is skipped.

last cannot be used to exit a block that returns a value (as it is the case with do {}, eval {}, or {}sub {}. It should not be used to exit a grep() or map() operation.

Syntax

[edit | edit source]
  last LABEL
  last EXPRESSION
  last

Examples

[edit | edit source]
  LINE: while (STDIN) {
    last LINE if /^$/;  # exit when done with header
    #...
  }

See also

[edit | edit source]
Previous: kill Keywords Next: lc