What is the 'Too many arguments for subroutine' error in Perl?

Perl’s “Too many arguments for subroutine” error is a compile-time error encountered when a subroutine is provided with more arguments than expected. It prevents code compilation.

The process is illustrated below:

Error format

The format of the "Too many arguments for subroutine" error is shown below:

Too many arguments for %s at %s, near %s

Each placeholder, %s, represents the subroutine name, the line number where the error occurred, and the argument that caused the error, respectively.

Example

The following example shows how the "Too many arguments for subroutine" error arises in Perl:

#!/usr/bin/perl
# Subroutine to add two numbers
sub findSum ($$) {
my ($p1, $p2) = @_;
print ("The sum is ", $p1 + $p2, ".\n");
}
# Valid call to subroutine
findSum(10, 20);

First, a subroutine named findSum that expects 22 parameters is initialized. The code proceeds to call the findSum subroutine with 22 arguments in line 1010. Since the number of provided arguments matches the expected parameters, the code executes without any errors.

However, if more than 22 arguments are provided to the findSum subroutine, then the "Too many arguments for subroutine" error is thrown, and the code fails to compile, as shown below:

#!/usr/bin/perl
# Subroutine to add two numbers
sub findSum ($$) {
my ($p1, $p2) = @_;
print ("The sum is ", $p1 + $p2, ".\n");
}
# Invalid call to subroutine
findSum(10, 20, 30);

The code above attempts to call the findSum function with 33 arguments, which results in an error. The error alerts the user to the subroutine name, the line number where the error occurred, and the argument that caused the error, i.e., main::findSum, line 1010, and 30, respectively. As a result, the code fails to compile.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved