Perl Tutorial

Subroutines

Defining functions and handling arguments in Perl

Defining Subroutines

In Perl, functions are called Subroutines and are defined using the sub keyword.
perl
sub greet {
    my $name = shift; # Get the first argument
    print "Hello, $name!\n";
}

greet("Alpha");

Handling Arguments (@_)

Wait! Perl doesn't have named parameters in the subroutine definition. Instead, all arguments are passed in a special array called @_.
There are two common ways to get arguments:
  1. shift: Gets the first item and removes it from @_.
  2. Assignment: Take all items at once.
perl
sub add {
    my ($a, $b) = @_;
    return $a + $b;
}

Return Values

A subroutine returns the value of the last expression evaluated, or you can use the explicit return keyword.

Private Variables (my)

Always use my inside a subroutine to declare variables. This ensures they are local to the function and don't overwrite variables with the same name elsewhere in your script.

Summary

TermDescription
subKeyword to declare a subroutine
@_The array containing all arguments
shiftGetting the next argument from @_
myDeclaring a local variable
returnExplicitly returning a value
Best ForOrganizing code and logic
Logic"Functional blocks of reusable code"
SafetyLexical scoping with my is essential
ModernPerl 5.20+ introduced optional "Experimental Signatures"
StandardUse snake_case for subroutine names
IdentitySubroutines can be called with &name() or just name()