simple use of eval of somethinng else

B

Billy Patton

I have a string
$expecxted_status = "!= 0";
$status = 0;

What I'm trying to do is

if ($status != 0) {
....
}


How do I get the $expected_status to evaluate to the boolean that it is, in
relation to $status?


$a = '== 0';
$b = '2';
eval { "$b $a" }; warn $@ if $@;

The above does nothing.
 
C

Ch Lamprecht

Billy said:
I have a string
$expecxted_status = "!= 0";
$status = 0;

What I'm trying to do is

if ($status != 0) {
...
}


How do I get the $expected_status to evaluate to the boolean that it is, in
relation to $status?

use strict;
use warnings;
my $s = '==1';
my $t = '0';
my $result = eval $t.$s ? 1:0;
print $t,$s,' evaluates to ',$result;

If you describe your task , you most probably will get answers that are more
useful than this.

Christoph
 
M

Mirco Wahab

Billy said:
How do I get the $expected_status to evaluate to the
boolean that it is, in relation to $status?

$a = '== 0';
$b = '2';
eval { "$b $a" }; warn $@ if $@;

The above does nothing.

use strict;
use warnings;

my $s1 = '== 0';
my $s2 = '2';

print eval( "$s2 $s1" ) ? 'true' : 'false';
print "\nerror $@" if $@;

see: perldoc -f eval
(EXPR eval)

Regards

M.
 
X

xhoster

Billy Patton said:
I have a string
$expecxted_status = "!= 0";
$status = 0;

What I'm trying to do is

if ($status != 0) {
...
}

How do I get the $expected_status to evaluate to the boolean that it is,
in relation to $status?

$a = '== 0';
$b = '2';
eval { "$b $a" }; warn $@ if $@;

You are using the block eval on something which just happens to be
a string. You probably want to do a string eval:

my $result = eval "$b $a"; warn $@ if $@;
print something_to_do_with($result);
The above does nothing.

It does plenty. It prints nothing. Which is expected, as you
haven't told it to print anything.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top