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

by
Jeremy Canfield |
Updated: December 01 2021
| 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.
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