in reply to Inputing an Operator is it possible?
another way you could implement this is using a hash of subroutine references, like so:
This has the advantage that you can define arbitrary operators, such as:use strict; my $operator=<STDIN>; my $op_map={}; my $one=10; my $two=5; chomp $operator; define_op_map(); if ( defined ($op_map->{$operator}) ) { &{$op_map->{$operator}}; } else { print "This op is undefined. Executing it might end the universe as +we know it.\n"; } sub define_op_map { $op_map->{'+'}=sub{print $one + $two . "\n"}; $op_map->{'-'}=sub{print $one - $two . "\n"}; $op_map->{'*'}=sub{print $one * $two . "\n"}; $op_map->{'/'}=sub{print $one / $two . "\n"}; }
A more complicated way to do the above is to define the subroutine references in an array and then fill the op_map hash by looping over this array. That way extending the functionality of the thing is cleaner.$op_map->{'camel'}=sub{print "$one is a $two with humps.\n"};
CU
Robartes-
|
|---|