Like everyone has pointed out "code" tags will make your code clearer.
Here is what I get when I clean out your code
use warnings;
use strict;
sub AddNumbers {
numberList($first_number, $second_number, $third_number, $fourth_n
+umber, $fifth_number, $sixth_number) = @_;
$add $first_number + $second_number + $third_number + $fourth_numb
+er + $fifth_number + $sixth_number;
}
print "Equals to: &add /n"
And here are the errors that your code generates:
Scalar found where operator expected at temp.pl line 7, near "$add $fi
+rst_number"
(Missing operator before $first_number?)
Global symbol "$first_number" requires explicit package name at temp.p
+l line 6.
Global symbol "$second_number" requires explicit package name at temp.
+pl line 6.
Global symbol "$third_number" requires explicit package name at temp.p
+l line 6.
Global symbol "$fourth_number" requires explicit package name at temp.
+pl line 6.
Global symbol "$fifth_number" requires explicit package name at temp.p
+l line 6.
Global symbol "$sixth_number" requires explicit package name at temp.p
+l line 6.
Global symbol "$add" requires explicit package name at temp.pl line 7.
syntax error at temp.pl line 7, near "$add $first_number "
Global symbol "$first_number" requires explicit package name at temp.p
+l line 7.
Global symbol "$second_number" requires explicit package name at temp.
+pl line 7.
Global symbol "$third_number" requires explicit package name at temp.p
+l line 7.
Global symbol "$fourth_number" requires explicit package name at temp.
+pl line 7.
Global symbol "$fifth_number" requires explicit package name at temp.p
+l line 7.
Global symbol "$sixth_number" requires explicit package name at temp.p
+l line 7.
Execution of temp.pl aborted due to compilation errors.
Let me try explaining what these errors mean:
Scalar found where operator expected at temp.pl line 7, near "$add $fi
+rst_number"
You will see that you have missed out the '='. Perl is telling you that it was looking for some operator but found another variable.
Global symbol "something" requires explicit package name at
Here you need to use 'my' because you have used "strict". If not Perl is going to look for places where this variable has been defined and not finding any will croak.
print "Equals to: &add /n"
First off new line is '\n' and not'/n'. I am unclear on what you expect printed here considering you have not called 'AddNumbers'
Here is your code with the above mentioned errors corrected:
use strict;
use warnings;
sub AddNumbers {
my ($first_number, $second_number, $third_number, $fourth_number,
+$fifth_number, $sixth_number) = @_;
my $add = $first_number + $second_number + $third_number + $fourth
+_number + $fifth_number + $sixth_number;
return $add;
}
my $sum = AddNumbers( 1, 2, 3, 4, 5, 6 );
print "The sum of input numbers equals to: $sum \n";
You must note that it is not a good idea to use a static number of params like you have. This will limit your method to a particular number of input parameters (in this case 6). The fact that Perl always passes parameters as a list can be exploited and allows for an easy way to implement polymorphism.
This is where the solution given by Anonymous Monk comes in. You will notice that that solution will work for any number of parameters.
|