Obviously other posts have covered the main points:
- you meant $_, not @_[$_]
- even if you'd wanted to index the array, the syntax is $array[$index], not @array[$index]
- 'use warnings' (or adding -w to your shebang line) should be your first step in troubleshooting (actually the zeroth step, you should always pre-emptively turn it on).
I'd just like to add that I think the problem also owes something to poor choice of variable names. I'm not one to say that using $_ and @_ is always evil, but compare how easy it is to spot the error:
sub total
{
my @numbers = @_;
my $total = 0;
foreach my $value (@numbers) {
$total += $numbers[$value]; # Wait, what?
}
return $total;
}