Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Subroutines, often called functions in other languages, play a crucial role in organizing and modularizing code in Perl. They allow for reusable blocks of code and enhance readability and maintainability.
A subroutine is a named block of code, possibly with parameters, that can be invoked from various parts of the program, allowing for code reuse and modular design.
Subroutines are defined using the sub
keyword followed by a subroutine name:
sub greet { print "Hello, World!\n"; }
Once a subroutine is defined, you can call it by its name:
greet(); # Outputs: Hello, World!
Arguments to a subroutine are passed through the default @_
array:
sub greet_person { my ($name) = @_; print "Hello, $name!\n"; } greet_person('Alice'); # Outputs: Hello, Alice!
Multiple parameters can be passed, and they will be stored in order in the @_
array:
sub greet_full { my ($first_name, $last_name) = @_; print "Hello, $first_name $last_name!\n"; } greet_full('Alice', 'Smith'); # Outputs: Hello, Alice Smith!
The return
keyword can be used to exit a subroutine and optionally send back a value:
sub add { my ($a, $b) = @_; return $a + $b; } my $sum = add(3, 4); print $sum; # Outputs: 7
By default, if there's no return
statement, a subroutine returns the value of the last evaluated expression.
my
Variables declared using the my
keyword within a subroutine are private to that subroutine:
sub example { my $private_var = "I am private"; print $private_var; # This will work } print $private_var; # This will produce an error
state
(with use feature 'state';
)While my
variables are re-initialized every time a subroutine is called, state
variables retain their value between calls:
use feature 'state'; sub count_calls { state $count = 0; $count++; print "Called $count times\n"; } count_calls(); # Outputs: Called 1 times count_calls(); # Outputs: Called 2 times
To pass arrays and hashes to subroutines, pass them by reference:
sub print_array { my ($arr_ref) = @_; foreach (@$arr_ref) { print "$_\n"; } } my @fruits = ('apple', 'banana', 'cherry'); print_array(\@fruits);
Subroutines in Perl:
@_
array.return
keyword.my
and persistent variables with state
.Understanding subroutines is foundational for writing efficient and organized Perl code.
Defining and calling functions in Perl:
sub greet { print "Hello, Perl!\n"; } # Calling the function greet();
Passing parameters to Perl subroutines:
sub greet { my ($name) = @_; print "Hello, $name!\n"; } # Calling the function with a parameter greet("Alice");
Return values from Perl functions:
sub add { my ($a, $b) = @_; return $a + $b; } # Calling the function and capturing the return value my $result = add(3, 5); print "Result: $result\n";
Recursive functions in Perl:
sub factorial { my ($n) = @_; return 1 if $n == 0; return $n * factorial($n - 1); } # Calling the recursive function my $result = factorial(5); print "Factorial: $result\n";
Anonymous subroutines in Perl:
my $greet = sub { my ($name) = @_; print "Hello, $name!\n"; }; # Calling the anonymous subroutine $greet->("Bob");
Scope of variables in Perl subroutines:
my $global_variable = "Global"; sub print_variable { my $local_variable = "Local"; print "Global: $global_variable\n"; print "Local: $local_variable\n"; } # Calling the subroutine print_variable();
Default parameters in Perl functions:
sub greet { my ($name, $greeting) = @_; $greeting //= "Hello"; # Default value if not provided print "$greeting, $name!\n"; } # Calling the function with and without the second parameter greet("Alice"); greet("Bob", "Hi");
Using prototypes in Perl subroutines:
sub add($$) { my ($a, $b) = @_; return $a + $b; } # Calling the function with strict parameter count my $result = add(3, 5); print "Result: $result\n";