How to call a subroutine in Perl?
Question
How to call a subroutine in Perl?
How to Call a Subroutine in Perl
In Perl, a subroutine (or sub) is a reusable block of code that you can call multiple times throughout your program. Calling a subroutine in Perl is straightforward, but understanding the syntax and some Perl-specific nuances can help you write clearer, more maintainable code.
Basic Syntax for Calling a Subroutine
You define a subroutine using the sub keyword followed by the subroutine name and a code block. To call or invoke a subroutine, use its name followed by parentheses containing any arguments you want to pass (or empty parentheses if none).
Example:
sub greet {
my ($name) = @_; # @_ holds the input arguments
print "Hello, $name!\n";
}
greet("Alice"); # Calling the subroutine with an argument
In this example:
sub greet { ... }defines the subroutine namedgreetgreet("Alice");calls the subroutine namedgreet, passing the string "Alice"- Inside the subroutine,
@_is the default array holding all arguments passed, here retrieving$nameas the first argument
Key Perl Concepts When Calling Subroutines
- Subroutine Names and Sigils: You usually call subs by bareword—just the name—without the
$,@, or%sigils that are used for variables. However, references to subs are invoked with the arrow operator->. - Argument Passing: All arguments to subs are passed in the array
@_. You typically assign these to lexical variables usingmy. - Return Values: Use
returnexplicitly or just have the desired value as the last evaluated expression. The calling code can capture this. - TMTOWTDI (There’s More Than One Way To Do It): Perl allows calling subs both with and without parentheses in many contexts, but it's clearer and safer to always use parentheses when passing arguments.
Complete Runnable Example
#!/usr/bin/perl
use strict;
use warnings;
# Define a subroutine that greets a person
sub greet {
my ($name) = @_; # Extract first argument
print "Hello, $name!\n";
}
# Call the subroutine with a name
greet("Alice");
# Another example: subroutine returns a value
sub add {
my ($x, $y) = @_;
return $x + $y; # Return sum
}
my $sum = add(5, 7);
print "5 + 7 = $sum\n";
# Calling a subroutine without arguments
sub say_hello {
print "Hello, world!\n";
}
say_hello();
Common Pitfalls
- Forgetting parentheses: Calling a subroutine without parentheses when it takes arguments can lead to confusing errors or unexpected behavior.
- Not unpacking
@_: Accessing arguments directly without assigning to lexical variables can make the code harder to read. - Using the wrong sigil: Subroutines are called by name without variable sigils; using them (like
$greet()) is incorrect unless you are using a code reference. - Context sensitivity: Subroutines may behave differently in scalar vs list context—be mindful when returning lists.
Overall, calling subroutines in Perl is simple but understanding the role of @_, the use of parentheses, and Perl's flexible syntax can make your code much clearer and less error-prone.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 4ms
Hello, Alice!
(empty)Was this helpful?
Related Questions
- How to use the return statement early in a Perl subroutine?
- How to create higher-order functions in Perl?
- How to use shift to get subroutine arguments in Perl?
- How to use caller() to get subroutine call information in Perl?
- How to call a subroutine as a method in Perl?
- How to use state variables in Perl subroutines?