in reply to Array of operators ...
You can't create a reference to an operator, but you can create a reference to a sub.
my @term = (4, 5); my @ops = ( [ '+' => sub { $_[0] + $_[1] } ], [ '-' => sub { $_[0] - $_[1] } ], [ '*' => sub { $_[0] * $_[1] } ], [ '/' => sub { $_[0] / $_[1] } ], ); for my $op (@ops) { my ($symbol, $coderef) = @$op; printf( "%s %s %s = %s\n", $term[0], $symbol, $term[1], $coderef->(@term), ); }
That's arguably a little cleaner and safer than using eval, but eval might be simpler and may be sufficient for your needs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array of operators ...
by TJPride (Pilgrim) on Sep 12, 2013 at 22:07 UTC |