Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

How does someone exit from a sub routine without exitting altogether? I know with arrays you can do LAST to stop the iterations. Is there a way to just stop processing a sub routing further by a type of exit command while not affecting the main script?

Replies are listed 'Best First'.
Re: How to exit out of a sub routine
by ikegami (Patriarch) on Sep 01, 2011 at 20:13 UTC

    How does someone exit from a sub routine without exitting altogether?

    Using return.

    sub doit { ... if (nothing_else_to_do) { return; } ... }

    If you want to exit with a catchable error, you can use die or croak.

    sub doit { ... if (did_not_provide_gizmo) { die("Gizmo required"); } ... }

    I know with arrays you can do LAST to stop the iterations.

    last (and next and redo) affect loops, not arrays. Specifically,

    while (and until) loops:

    while (...) { ... last; ... }

    C-style for loops, foreach loops and counting loops:

    for (...) { ... last; ... }

    Bare loops:

    { ... last; ... }
Re: How to exit out of a sub routine
by toolic (Bishop) on Sep 01, 2011 at 19:47 UTC