PDA

View Full Version : eval command weirdness


Jakobud
02-04-2005, 07:13 AM
eval "int $i = 3 + 2;";
print $i;



results in:

// Error: print $i; //
// Error: "$i" is an undeclared variable. //

What the heck am I doing wrong here?

failure
02-04-2005, 08:47 AM
$i is local to the anonymous procedure represented by the code in the string. Use global declaration to make $i global, therefore existent in both scopes.

global int $i;
eval( "int $i = 2 + 3;" );
print $i;

Jakobud
02-04-2005, 06:36 PM
Hmmm... okay. I find it very odd that the Maya help doesn't say ANYTHING about the eval command having it's own little scope.

Now if what you say is true, then why does this happen:

1. Run this script:

{
eval "int $i = 3 + 2;";
}

Okay, no errors here. NOW, try to run this simple command:

print $i;

It actually returns "5" as if "$i" was a global variable!? Why is this? If $i was a global variable, then the script listed in my first post would have worked fine...

If you could shed some light on this, I'd appreciate it.

ajk48n
02-04-2005, 07:12 PM
I believe the variable just has to be dcalred before you run the print command on it.

If you run

int $c;
eval "int $c = 3 + 2;";

print $c;

it will work. This might be the reason having the brackets around the eval command works. It forces Maya to evaluate the variable, and then the print command can read it.


Strangely though, having the eval command within the brackets should not work, since they should make any variables local to them.

markbones
02-04-2005, 10:19 PM
eval "int $i = 3 + 2;";
print $i;


results in:

// Error: print $i; //
// Error: "$i" is an undeclared variable. //

What the heck am I doing wrong here?

A test, referring to your original code: If I run the first line - the eval statement - I get a result. If I then run the print statement, I get the correct result. If I hilite them both and run them "at the same time" as one block, I get the same error as you.
If I run each line once independantly (so the variable has already been declared and is in memory), then I hilite them both and run both statements at the same time, I do not get the error.

If I create a little proc in the script editor and try and "source" it, I get the same errors:
global proc printValue()
{
eval "int $b= 3 + 8;";
print $b;
}

As previously mentioned, it probably has to do with scope, and IMHO I would consider it bad programming practise to declare your variable inside your eval statement. Better to declare it previously and only execute the assignment inside the eval statement.

Cheers!

failure
02-04-2005, 11:50 PM
Okay, I gave this problem a second tought. Although it seems like a scope thing it really isn't. The variable is global in both cases. So my previuos claim is not entirely true.

The problem is related to the way scripts are run ( not just mel, but any scripting language infact ).
When you execute a piece of code it goes in two stages; first the interpreter takes the source and "compiles" it to some kind of bytecode ( checking for compile-time errors ), and then, as phase two, it executes the bytecode. Take a look at:


eval "int $i = 3 + 2;";
print $i;


Here we actually need to compile and execute two scripts; "outer" script - having above source, and "inner" script which is one passed as a string argument to eval.
Now, in the "outer" script there is no explicite declaration of $i since at the "outer" script compile-time the "inner" script is not compiled/run yet and it exists only as a source code inside string literal. So when "outter" gets compiled it reports the undeclared variable - which is a complie-time error.

When you execute the two statements separately, the "inner" and "outer" scripts of eval statement are compiled and executed without errros. This establishes the $i as a valid global name. So when you run the print statement afterwards, $i is recognized without problems

CGTalk Moderation
02-05-2006, 12:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.