tags:

views:

158

answers:

5

I'm looking for a way to do this in Perl:

$a = "60"; $b = "< 80";

if ( $a $b ) {  then .... }

Here, $b "holds" an operator... can I do this? Maybe some other way?

+2  A: 
$a = "60"; $b = "< 80";
if( eval($a. $b)){
  print "ok";
}

see perldoc eval for more

ghostdog74
Thanks, worked :)
Ricky
+8  A: 

How about defining a function that wraps the needed condition:

my $cond = sub { $_[0] < 80 };

if ( $cond->( $a ) ) { 
     ...
}
friedo
+15  A: 

It's nice to see how people discover functional programming. :-)

Luckily, Perl has capabilities to create and store functions on-the-fly. For example, the sample in your question will look like this:

$a = "60"; $b = sub { $_[0] < 80 };

if ( $b->($a) ) { .... }

In this example, a reference to the anonymous subroutine is stored in $b, the sub having the same syntax for argument passing as a usual one. -> is then used to call-by-reference (the same syntax you probably use for references to arrays and hashes).

But, of course, if you want just to construct Perl expressions from arbitrary strings, you might want to use eval:

$a = "60"; $b = " < 80";

if ( eval ("$a $b") ) { .... }

However, doing this via eval is not safe, if the string you're eval-ing contains parts that come as user input. Sinan Ünür explained it perfectly in his answer-comment.

Pavel Shved
+7  A: 
Sinan Ünür
I wouldn't go as far as saying "you **must** always use strict and warnings", but, good answer/comment :)
thenduks
@thenduks: I would. One should *fully* understand the ramifications of not using either of those pragmas, and be able to justify the occasion. For beginners, it should not be tolerated.
Ether
I prefer not to talk in generalisms. There's no reason you can't write a one-off script without strict and warnings. It might not be tolerable for you (or, say, people you hire), but others are free to come to their own conclusions.
thenduks
@thenduks and if/when you write that script, and you have problems with it due to any of the issues identified by `strict` or `warnings`, and you post your question on a public forum, you will be reminded why you should have used them.
Sinan Ünür
Again, that's a general statement that may or may not apply to any given situation. Anyway, carry on.
thenduks
+1 @sinan: Or you could say: if/when you have problems with your script, just enable strict and warnings, before posting to a public forum.
Zano
@Zano the point is not to *appear* as if one is doing the right thing in public but to do the right thing when one is alone with the keyboard.
Sinan Ünür
@Sinan: You missed my point. Enabling -w and strict will reveal the problems 90% of the time. The idea that anyone would think that these pragmas are for appearance is just plain ridiculous.
Zano
@Zano You missed my point: I've seen cases on SO and other places where people slap a quick `use strict;` on a script (that would not actually compile with `use strict;`) just for appearance sake.
Sinan Ünür
@Sinan: Well, that wasn't what I was talking about. Oh well, I'll let you have the last word.
Zano
+1  A: 

I wonder if Number::Compare is of any interest here. From the example:

 Number::Compare->new(">1Ki")->test(1025); # is 1025 > 1024

 my $c = Number::Compare->new(">1M");
 $c->(1_200_000);                          # slightly terser invocation
justintime