www.perl.com
Perl Programming
Calling Subroutines
  • Subroutine calls usually have arguments in parentheses
    • Parentheses are not needed if sub is declared first
    • But using parentheses is often good style
  • Subroutine calls may be recursive
  • Subroutines are another data type
    • Name may be preceded by an & character
    • & is not needed when calling subs

        print factorial(5) . "\n";   # Parentheses required

        sub factorial {
            my ($n) = @_;
            return $n if $n <= 2;
            $n * factorial($n - 1);
        }

        print ((factorial 5) . "\n");    # Parentheses around argument not required,
			               # but need to ensure there are no extra arguments
        print &factorial(5) . "\n";  # Neither () nor & required
      
Previous http://stuff.mit.edu/iap/perl/