Details on Perl's Calling Conventions for Subroutines and Functions

If your subroutine or function definition does not include a parameter list or parentheses, there are three ways to call it:

  1. use subroutine name with parentheses: mysubroutine( argument1, argument2, ... )
  2. prepend subroutine symbol, & , to subroutine name with parentheses: &mysubroutine( argument1, argument2, ... )
  3. parentheses may be omitted under certain conditions (see below): mysubroutine, &mysubroutine, or mysubroutine parameter

Rules for calling subroutines without parentheses

  1. If a subroutine has been previously defined, parentheses may be omitted. The subroutine name then works like an operator. For this reason you can write
    print $myString;
    
    print( ) is a function defined in a module that loads before your program. The parentheses of the parameter list thus can be omitted and the string value following the print statement will passed as a parameter. User-defined functions work the same way. If your program consists of the following lines, it will print the "Hello" message:
    sub printMe
    {
      print "@_\n";
    }
    
    printMe "Hello function!\n";
    
    printMe( ) is defined before it is called and the string will be passed to the subroutine. The following code, however, will not execute:
    printMe "Hello subroutine!\n";
    
    sub printMe
    {
      print "@_\n";
    }
    
    Here printMe "string" is called before the subroutine is defined. (The interpreter reads the program line by line.) This will create an compilation error.
  2. Likewise,
    sub printMe()
    {
      print "Hello\n";
    }
    
    printMe;
    
    will work (subroutine is defined before it's called), but
    printMe;
    
    sub printMe()
    {
      print "Hello\n";
    }
    
    
    will not.
  3. However, if a subroutine call includes the parentheses, or if it is prepended with the subroutine symbol, &, or both, the subroutine can be called before it is defined.
    printMe();
    
    sub printMe()
    {
      print "Hello\n";
    }
    
    will not crash, and neither will
    &printMe;
    
    sub printMe()
    {
      print "Hello\n";
    }
    
    
    The reason for this is as follows: If the subroutine call does not include parentheses or the prepended subroutine symbol, Perl will look upon the subroutine name as an operator. If the subroutine hasn't been defined yet, it doesn't know what to do with it. However, Perl allows you to define subroutines anywhere in the program. So, if you append parentheses or prepend the subroutine symbol to the subroutine name, Perl knows that this statement is supposed to be a subroutine call and waits until all subroutines are loaded.

Confused? There's good reason to be confused. Doing one thing n different ways is not good programming practice. Recommendation: Be explicit and consistent. Always include explicit subroutine declarations in your code and call subroutines as subroutines, not just by name. It will make your code more readable and a lot easier to maintain.