in reply to extract an operator from a string
#!/usr/bin/perl -w use strict; my %ops = ( '+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '*' => sub { $_[0] * $_[1] }, '/' => sub { $_[0] / $_[1] } ); print $ops{$ARGV[1]}->($ARGV[0], $ARGV[2]), "\n";
Of course, if you want to support more than just a single binary operation you'll need to parse the expression. For that I'd use Parse::RecDescent. In fact - I did! You can check out HTML::Template::Expr for a rather elaborate example of this technique.
-sam
|
---|