in reply to Simple add and multiply subroutines
So the range operator lets you say:
1..5
which is equivalent to the list:
1, 2, 3, 4, 5
So if, say, your ARGV array in the original example was ('add', 1, 7, 22, 5) then $ARGV[1]..$ARGV[$i] would evaluate to 1..5, ie: (1, 2, 3, 4, 5). Echoing oko1's comment about a dispatch table being the way to go, here is how I would write your script:
use warnings; use strict; my %dispatch = ( add => sub { my $sum = 0; $sum += $_ for @_; $sum; }, multiply => sub { my $prod = 1; $prod *= $_ for @_; $prod; }, ); my $op = shift @ARGV; print $dispatch{$op}->(@ARGV);
|
|---|