You're trying to modify read-only values (as Perl has no doubt told you).
Inside your subroutine, @_ contains three values (5, 6 and 7). But they aren't just values, they are the constants 5, 6 and seven. And in the foreach loop, $_ is aliases to each of those constants in turn. So when you try to assign a new value to $_, you are trying to overwrite the value of a constant. Which Perl, very sensibly, prevents you from doing.
What you really want is something like this:
sub circ_area { my @radii = @_; # copies values into a variable foreach (@radii) { $_= 3.14 * ($_**2); } return @radii; }
Or, more Perlishly:
sub circ_area { return map { 3.14 * ($_**2) } @_; }
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re: subroutine array question
by davorg
in thread subroutine array question
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |