On Nov 17, 9:47 am, [EMAIL PROTECTED] (Richard Lee) wrote:
> say something like == or eq ..
>
> Can you sub them w/ varilable like $unknown   ?
>
> Let me be more specific.
> Let's say I don't know what the variable will hold
>
> I guess I can say something like,
>
> sub check_unknown {
>         my $unknown = shift;   ## it's either digits or letters but both
> will be same kind
>         my $unknown1 = shift; ## it's either digits or letters
>
>        my $result = ( $unknown =~ /^\d+$/ ) ? '==' : 'eq';
>         if ( $unknown $result $unknown1 ) {
>                      do something...
>         }
>
> }
>
> But obviously above dont work.. can someone shed some light on this?

Perl has an evil, er, eval() function but you really should *NOT* use
eval("\$unknown $result \$unknown1") for this.

In this simple case write it long hand:

if ( ( $unknown =~ /^\d+$/ ) ? ($unknown == $unknown1)  :  ($unknown
eq $unknown1))

In more complex cases you'd should resort to a dispatch table.

my %ops = map { $_ => eval "sub { \$_[0] $_ \$_[1] }" } '==', 'eq';

my $result = ( $unknown =~ /^\d+$/ ) ? '==' : 'eq';
if ( $ops{$result}->($unknown,$unknown1) ) {


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to