RE: operator question

2002-06-11 Thread wayne

On 5 Jun 2002 at 17:18, Morse, Richard E. wrote:

> Charles Oppenheimer [mailto:[EMAIL PROTECTED]] wrote:
> 
> > Hi folks.  I have a question... I wanted to create a
> > sub procedure that takes an operator as an argument,
> > like so:


> I've never actually done this, so I'm somewhat guessing here, but you
> might be looking for something like:
> 
> sub compare {
>  my ($a, $o, $b) = @_;
>  if (eval("$a $o $b")) {
>   print "True!\n";
>  }
> }
> 

you also might want to try this, if speed is an issue, it is 
significantly faster:

sub get_template_compare
{
my ($operator) = @_;

return eval qq
{
sub { \$_[0] $operator \$_[1] }
} 
||
die "Template Code compilation failed\n";   
}

*template_compare = get_template_compare('<');


# check logic:

die unless template_compare(1,2);
die if template_compare(3,2);





___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



RE: operator question

2002-06-05 Thread Morse, Richard E.

Charles Oppenheimer [mailto:[EMAIL PROTECTED]] wrote:

> Hi folks.  I have a question... I wanted to create a
> sub procedure that takes an operator as an argument,
> like so:
> 
> &compare(1,"<",2);
> 
> sub compare {
>   my ($value1,$operator,$value2) = @_;
> 
>   if ($value1 $operator $value2) {
>   print "statement is true!!\n";
>   }
> 
> }

I've never actually done this, so I'm somewhat guessing here, but you might be
looking for something like:

sub compare {
my ($a, $o, $b) = @_;
if (eval("$a $o $b")) {
print "True!\n";
}
}

Note that this is completely untested -- you may need to work a bit harder to
get the eval block to return the value correctly...

HTH,
Ricky
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs