How to call a Perl subroutine (notes on the ampersand operator)

Recently I wrote a Perl subroutine tutorial, and a few people (correctly) gave me grief over using the ampersand operator ("&") when calling my Perl subroutines. I'm not going to spend a lot of time on this, but I think it's important for people to know when they have to use the ampersand operator when calling their subroutines.

Perl subroutines and the ampersand operator

In short, you need to place an ampersand before a call to your custom Perl subroutine if the call to the subroutine appears before the definition of the subroutine.

For example, if I want to call my subroutine before I actually define it, I need to use the ampersand character before my subroutine call. Specifically, this Perl script will work as expected (because I use the ampersand operator):

# this works; the '&' tells perl to look for a subroutine named 'hello'
&hello;

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

and this Perl script will not work as expected:

# this does not work; perl hasn't seen the 'hello' subroutine yet
hello;

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

Modern Perl subroutine practices

Because of modern Perl programming practices, the ampersand operator is typically not needed (because we all tend to declare our Perl subroutines/functions before using them), but I just want to be clear about this point.

Getting to what several people wrote me about, it is common practice to call your Perl subroutines as follows, without the ampersand operator preceding your function call. (It is now common practice to do this, but it wasn't when I first wrote the article in the 1990s.) Here's an example:

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

# note: no ampersand needed here
hello;

Perl interpreter warnings

As a final note, if I modify my first Perl script to use warnings, like this:

#!/usr/bin/perl -w

hello;

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

and then try to run it, the Perl interpreter gives me the following warning messages:

Unquoted string "hello" may clash with future reserved word at ./sub1.pl line 3.
Useless use of a constant in void context at ./sub1.pl line 3.

The "Useless use of a constant" message refers to this line:

hello;

which Perl can't figure out what to do with (because it is an interpreter, and it hasn't run across the definition of the 'hello' function yet). When I add an ampersand in front of the call to the hello function, like this:

&hello;

the warning messages go away.

My earlier Perl subroutine (sub) tutorial

For reference purposes, here's a link to my original Perl subroutine (sub) tutorial.

Follow-up note

As one follow-up note, another approach here is to declare your Perl subroutines at the top of your Perl file, as shown in this somacon.com code. I'm in a rush now, but I'll come back and address this more in the near-future.

Or just use sub()

Another way to make it clear you're calling a subroutine, without using the ancient "&", is to append () when calling the sub... like most languages do:

#!/usr/bin/perl -w

hello();

sub hello {
print "Hello\n";
}

That way you don't have to worry about whether your subroutine is earlier or later in the file.

Post new comment

The content of this field is kept private and will not be shown publicly.