in reply to Simple add and multiply subroutines
Not that I'm disagreeing with all the errors/problems other people have pointed out, but this is really the canonical case for using a dispatch table.
#!/usr/bin/perl -w use strict; my $op = shift or die "You must specify an operation followed by numbe +rs.\n"; die "Non-numeric input.\n" if grep /\D/, @ARGV; my %perform = ( add => sub { local $" = "+"; eval "@ARGV"; }, multiply => sub { local $" = "*"; eval "@ARGV"; }, ); die "Invalid operation.\n" unless defined $perform{$op}; print $perform{$op}->();
-- Human history becomes more and more a race between education and catastrophe. -- HG Wells
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Simple add and multiply subroutines
by jwkrahn (Abbot) on Apr 08, 2008 at 06:35 UTC | |
by oko1 (Deacon) on Apr 08, 2008 at 20:05 UTC |