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
In Perl, a function or subroutine is a reusable chunk of code that can be called by its name. Unlike some other languages, Perl does not have a strict signature system for subroutines out of the box. However, with the use of specific modules, you can get more type-specific behavior. In this tutorial, we'll cover the basics of function signatures and then introduce a method to use stricter signatures with the help of the signatures
feature.
In Perl, a basic subroutine is defined using the sub
keyword followed by the subroutine name:
sub hello { print "Hello, World!\n"; }
You can call the subroutine using its name followed by parentheses:
hello(); # prints "Hello, World!"
Perl subroutines accept a list of parameters in the @_
array:
sub greet { my ($name) = @_; print "Hello, $name!\n"; } greet('Alice'); # prints "Hello, Alice!"
If you want to pass multiple parameters, you can do so and unpack them accordingly:
sub greet_full { my ($first_name, $last_name) = @_; print "Hello, $first_name $last_name!\n"; } greet_full('Alice', 'Smith'); # prints "Hello, Alice Smith!"
signatures
FeatureStarting from Perl 5.20, you can enable the experimental feature called signatures
which provides a cleaner way to declare subroutine parameters:
use feature 'signatures'; no warnings 'experimental::signatures'; sub greet($first_name, $last_name) { print "Hello, $first_name $last_name!\n"; } greet('Alice', 'Smith'); # prints "Hello, Alice Smith!"
Note:
signatures
Feature@_
to individual variables.While Perl doesn't enforce strict type signatures for subroutines in its base form, you can adopt cleaner and more readable syntax with the signatures
feature. This can make your code more maintainable and easier to understand for others (or yourself when revisiting it later). However, always check the current Perl documentation to see if there have been any updates or changes related to the signatures
feature.
Defining subroutines with signatures in Perl:
use feature 'signatures'; no warnings 'experimental::signatures'; sub greet($name) { print "Hello, $name!\n"; } greet("Alice");
Parameters and arguments in Perl function signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub add($num1, $num2) { return $num1 + $num2; } my $result = add(3, 5); print "Result: $result\n";
Using signatures for subroutine declarations in Perl:
sub
keyword along with signatures for parameter definition.use feature 'signatures'; no warnings 'experimental::signatures'; sub multiply($a, $b) { return $a * $b; } my $result = multiply(4, 6); print "Result: $result\n";
Named parameters in Perl function signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub greet_person($name, $greeting = "Hello") { print "$greeting, $name!\n"; } greet_person("Bob"); greet_person("Alice", "Good morning");
Default values in Perl subroutine signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub greet_person($name, $greeting = "Hello") { print "$greeting, $name!\n"; } greet_person("Bob"); greet_person("Alice", "Good morning");
Required and optional parameters in Perl signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub calculate($num1, $num2 = 0) { return $num1 * $num2; } my $result1 = calculate(5); my $result2 = calculate(3, 7); print "Result 1: $result1\n"; print "Result 2: $result2\n";
Passing arrays and hashes in Perl function signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub process_data(@numbers) { my $sum = 0; foreach my $num (@numbers) { $sum += $num; } return $sum; } my $result = process_data(1, 2, 3, 4); print "Result: $result\n";
Type constraints in Perl subroutine signature:
use feature 'signatures'; no warnings 'experimental::signatures'; sub multiply($a, $b) { die "Parameters must be numbers" unless (looks_like_number($a) && looks_like_number($b)); return $a * $b; } my $result = multiply(4, 6); print "Result: $result\n";
Perl function signatures vs traditional subroutines:
# Traditional subroutine sub add_numbers { my ($num1, $num2) = @_; return $num1 + $num2; } my $result1 = add_numbers(3, 5); # Subroutine with signature use feature 'signatures'; no warnings 'experimental::signatures'; sub add($num1, $num2) { return $num1 + $num2; } my $result2 = add(3, 5); print "Result 1: $result1\n"; print "Result 2: $result2\n";