Details on Perl's Calling Conventions of Functions

If your function definition (the function prototype) does not includes a parameter list or parentheses, there are three ways to call a function:

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

Rules for calling functions without parentheses

  1. If a function has been previously defined, parentheses may be omitted. The function 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 function. The following code, however, will not execute:
    printMe "Hello function!\n";
    
    sub printMe
    {
      print "@_\n";
    }
    
    Here printMe "string" is called before the function 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 (function is defined before it's called), but
    printMe;
    
    sub printMe()
    {
      print "Hello\n";
    }
    
    
    will not.
  3. However, if a function call includes the parentheses, if it is prepended with the function symbol, &, or both, the function 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 function call does not include parentheses or the prepended function symbol, Perl will look upon the function name as an operator. If the function hasn't been defined yet, it doesn't know what to do with it. However, Perl allows you to define functions anywhere in the program. So, if you append parentheses or prepend the function symbol to the function name, Perl knows that this statement is supposed to be a function call and waits until all functions 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 function prototypes in your code and call functions as functions, not just by name. It will make your code more readable and a lot easier to maintain.