eval statement in PERL

S

smartbadge

Hi All

I am having trouble using the eval statement.

I want eval to execute a Boolean string like the following example:

$str = " (!1 && 0) || (0 && 1)";
eval($str);

However this is not giving me consistent results. Sometimes it works
and returns the correct answer, other times it returns nothing. Any
help on this would be appreciated. Maybe ye have another way of
dynamically running Boolean strings??

Smartbadge
 
A

Anno Siegel

Hi All

I am having trouble using the eval statement.

I want eval to execute a Boolean string like the following example:

$str = " (!1 && 0) || (0 && 1)";
eval($str);

Why do you think you want eval for a simple boolean expression?
However this is not giving me consistent results. Sometimes it works

From the code you show, you wouldn't know about results. You're not
keeping the result of eval().
and returns the correct answer, other times it returns nothing. Any
help on this would be appreciated. Maybe ye have another way of
dynamically running Boolean strings??

Are you saying you run a program similar to the above multiple times
and get different results each time? That's hard to believe.

If not, what's the difference between the runs?

Show code that we can run that demonstrates the behavior. Explain what
you expect it to do and how that it different from what actually
happens.

Anno
 
S

Sisyphus

Hi All

I am having trouble using the eval statement.

I want eval to execute a Boolean string like the following example:

$str = " (!1 && 0) || (0 && 1)";
eval($str);

However this is not giving me consistent results. Sometimes it works
and returns the correct answer, other times it returns nothing.

For me that eval() always returns 0.
Can you show us the code that demonstrates that the eval() returns
inconsistent results ? (The code you have shown doesn't display the return
value at all.)

Cheers,
Rob
 
S

smartbadge

Let me re-phrase

Try the following:

$str = " (1 && 0) || (!0 && !1)";
$answer = eval($str);
print "the result of the boolean string is $answer\n";

What do you get for $answer?
Why do you think you want eval for a simple boolean expression?

This is a boolean expression in the form of a string. What other
options do I have?


Smartbadge
 
A

Anno Siegel

Let me re-phrase

Rephrase what?

Please give an attribution and quote some context when you reply.
Try the following:

Always use strict and warnings when you are testing Perl code.
$str = " (1 && 0) || (!0 && !1)";
$answer = eval($str);
print "the result of the boolean string is $answer\n";

What do you get for $answer?

A booleans "false" as expected. It prints as an empty string. All
perfectly normal.
This is a boolean expression in the form of a string. What other
options do I have?

Put it directly in your source, instead of a string.

Anno
 
S

smartbadge

Okay,

I was un-aware of the empty string as being
a legitimate result from the eval statement.

Thank you for your assistance.

Smartbadge
 
S

Sisyphus

Let me re-phrase

Try the following:

$str = " (1 && 0) || (!0 && !1)";
$answer = eval($str);
print "the result of the boolean string is $answer\n";

I think you can better demonstrate what you mean, and in terms that don't
involve eval(), with the following:

use warnings;
use strict;
my $answer = (1 && 0) || (!0 && !1);
print "the result of the boolean string is $answer\n";
$answer = (!1 && 0) || (0 && 1);
print "the result of the boolean string is $answer\n";
__END__

For me that prints:
the result of the boolean string is
the result of the boolean string is 0

I think it's just that a boolean false can appear either as an undef (empty
string) or a zero - both evaluate as "False", and both are equivalent to
zero.

Cheers,
Rob
 
A

Anno Siegel


Who are you talking to?
I was un-aware of the empty string as being
a legitimate result from the eval statement.

Thank you for your assistance.

Your thanks would be more welcome if you had heeded my advice
Please give an attribution and quote some context when you reply.

Your behavior is rude.

Anno
 
S

smartbadge

Anno, Rob

Thank to everyone who assisted me today.

The matter is closed for now.

Smartbadge
 
M

Matija Papec

X-Ftn-To: Anno Siegel

Put it directly in your source, instead of a string.

I assume that he reads expressions from file, so eval looks like valid
choice.
 
J

Joe Smith

Let me re-phrase

Hey, dude, not everybody reads USENET newsgroups using Google.
Just because you can see two or more postings at the same time does
not mean we do.

When posting to the newsgroups, you should *not* assume that your
followup and the previous article are both on screen at the same time.
With one posting per screen, you've got to quote part of the
article you're responding to, as I have done.
-Joe
 
S

smartbadge

Joe said:
Hey, dude, not everybody reads USENET newsgroups using Google.
Just because you can see two or more postings at the same time does
not mean we do.

When posting to the newsgroups, you should *not* assume that your
followup and the previous article are both on screen at the same time.
With one posting per screen, you've got to quote part of the
article you're responding to, as I have done.
-Joe

Joe

That is a fair point. I will do this in future

smartbadge
 

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
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top