Perl subroutines - a Perl subroutine (sub) tutorial

Perl subroutines FAQ - As a developer, when you start working with subroutines in Perl, you'll probably have the same questions I did:

  • How do I define a Perl subroutine?
  • How do I call a Perl subroutine?
  • How do I pass arguments to a Perl subroutine?
  • How do I access arguments in a Perl subroutine?
  • How do I return a value from a Perl subroutine?

In this article I'll try to briefly cover each of these Perl subroutine questions.

A simple Perl subroutine (sub)

To define a simple Perl subroutine, just use the following Perl "sub" syntax:

sub hello
{
  print "Hello, world.\n";
}

As you can see, this simple Perl subroutine (function) should print "Hello, world." when it is called.

Calling a Perl subroutine

To call this simple Perl subroutine, just use this syntax:

hello;

Putting these two pieces of code together, we currently have a Perl script that looks like this:

sub hello
{
  print "Hello, world.\n";
}

hello;

If this script is named hello.pl, we can run it from the command line like this:

perl hello.pl

When you run it, the 'Hello, world.' string shown will be printed.

Perl subroutines - Accessing subroutine arguments

Once you've created a simple Perl subroutine that takes no arguments, you'll want to be able to create one that does take arguments.

For our purposes, we'll extend our current Perl function to take one argument, and we'll print that argument. Here's what this new subroutine looks like:

# a simple perl subroutine.
# expect a person's name to be passed in, then print hello to them.
sub hello
{
  my $name = $_[0];
  print "Hello, $name.\n";
}

Arguments to Perl subroutines are made available via the special @_ array. Therefore, when you need to access the first element passed in to your Perl subroutines, you use the $_[0] syntax, as shown in that example. The second argument to your Perl sub is accessed with the $_[1] element, and so on.

You can also access Perl subroutine arguments using the shift operator, like this:

# a simple perl subroutine.
# expect a person's name to be passed in, then print hello to them.
sub hello
{
  my $name = shift;
  print "Hello, $name.\n";
}

Either approach to accessing Perl subroutine arguments can be used. The shift approach is generally easier to use and remember (so I probably should show it here first), but I wanted to show the numbered array example so you can see more of what's going on here.

Oops, I almost forgot: The Perl "my" operator makes the variable after the my keyword private to the Perl subroutine. So, in the example shown above, this code:

my $name

makes the variable $name local in scope to the hello function. To be more clear, you will not be able to access the $name variable outside of the hello function because I have declared $name with the Perl my operator, as shown.

Passing arguments to a Perl sub (subroutine)

To pass an argument to a Perl subroutine, just add the argument to the subroutine call like you normally would when calling any Perl function:

hello('Alvin');

If my subroutine was written to take both the first name and last name of the user (it currently is not), the subroutine call would now look like this:

hello('Alvin', 'Alexander');

Perl subroutines - How to return values

Perl subroutines can also return values when they are called. To return one parameter to the calling program, your subroutine can look like this:

sub hello
{
  my $name = $_[0];
  return "Hello, $name.\n";
}

An interesting thing about Perl subroutines is that the Perl return operator is optional. You can write that same subroutine without the return operator, as shown below, and in fact, most Perl developers seem to do this:

sub hello
{
  my $name = $_[0];
  "Hello, $name.\n";
}

I think the code is more clear with the Perl return operator, but again, it is optional.

Getting return values from Perl subroutines

Now that our hello subroutine returns a string, our calling program needs to be modified to receive that string. Here's how I would now call this new version of the hello subroutine:

$string = hello('Al');

More on Perl subroutines

There's much more to say about subroutines in Perl, but I hope this will help get you started down the Perl subroutine (sub) path.

If you have any questions, or would like to see more Perl subroutine tutorials, just leave a comment below, or send me an email, and I'll be glad to write more.