in reply to string to expression

Hi xiaoyafeng,

You could use a dispatch table.

For example:

my $pdispatch = { '+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '*' => sub { $_[0] * $_[1] }, '/' => sub { $_[0] / $_[1] }, }; # and later ... my $operator = qw(+ - * /)[int rand 4]; my $true_answer = $pdispatch->{$operator}->($value1, $value2);

The dispatch table $pdispatch is a hash reference, where each key evaluates to an anonymous subroutine to perform that operation on its two arguments.

The operator $operator is randomly selected from a list of 4 operators, and $true_answer is assigned the results of the subroutine for the random operator, performed on its 2 arguments.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/