Bootstrap FreeKB - Perl (Scripting) - Break out of a subroutine (return)
Perl (Scripting) - Break out of a subroutine (return)

Updated:   |  Perl (Scripting) articles

Typically, one of the following is used to break out of a loop or subroutine, or to move onto the next item in a loop.

  • last (break out of a loop)
  • next (move onto next item in a loop)
  • return (break out of a subroutine)

 

If you are not familiar with subroutines, check out our article on Perl - Getting Started with Subroutines.

return will cease execution of a subroutine and return you to your main routine. Take for example the following script. This will print Hello but will not print World, since you are returned before World is printed.

#!/usr/bin/perl
use strict;
use warnings;

sub greeting {
  print "Hello \n";
  return;
  print "World \n";
}

greeting();

 

Running this script will output the following.

Hello

 

You can also use return to:

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 144f59 in the box below so that we can be sure you are a human.