Perl (Scripting) - Getting Started with Subroutines

by
Jeremy Canfield |
Updated: March 19 2020
| Perl (Scripting) articles
A subroutine is a collection of reusable markup. In this example, there is a subroutine named "foo" that will print "Hello World";
sub foo {
print "Hello World\n";
}
In this example, the following markup will invoke the subroutine, which will print "Hello World".
foo();
Let's consider this example.
sub foo {
$bar = "Hello World";
}
Since print was not used in the subroutine, we need to do this to print "Hello World".
my $foo = foo();
print "$foo\n";
Or, even better, let's return $bar.
sub foo {
$bar = "Hello World";
return $bar;
}
Now we can call the subroutine and print $bar.
foo();
print "$bar\n";
Did you find this article helpful?
If so, consider buying me a coffee over at