"last" operator for functions?

Does Perl have an operator to exit a function or lastfor functions?

sub f {

    # some code here

    if ($v == 10) {
    # goto end of function/exit function/last
    }

    # some code here
}

A gotocould do the trick, but is he somehow mistaken?

+5
source share
2 answers

Use return;to exit a routine.

+16
source

Well, ... you can use gototo skip to the end of the routine:

sub f {

  # some code here

  if ($v == 10) {
    goto END;
  }

  # some code here
  END:
}

Or use lastto skip to the end of the routine (if you add a block):

sub f {
  END: {

    # some code here

    if ($v == 10) {
      last END;
    }

    # some code here

  } # END
}

What you really want to use return

sub f {

  # some code here

  if ($v == 10) {
    return;
  }

  # some code here

}

If you want to know what features are available, I would check the perlfunc page .

0
source

All Articles