Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple use of eval of somethinng else

Reply
Thread Tools

simple use of eval of somethinng else

 
 
Billy Patton
Guest
Posts: n/a
 
      02-20-2007
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.


 
Reply With Quote
 
 
 
 
Ch Lamprecht
Guest
Posts: n/a
 
      02-20-2007
Billy Patton wrote:
> 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

>
>
> $a = '== 0';
> $b = '2';
> eval { "$b $a" }; warn $@ if $@;
>
> The above does nothing.
>
>



--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(J ust=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      02-20-2007
Billy Patton wrote:
> 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.
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-20-2007
"Billy Patton" <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
eval('07') works, eval('08') fails, why? Alex van der Spek Python 6 01-08-2009 08:24 PM
Different behavior between eval "07" and eval "08" Liang Wang Perl Misc 8 02-02-2008 08:31 PM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments