The eval Command

An extremely useful command.  Eval allows you to execute a script that
you dynamically generate from within another script.

It can be used for efficiency's sake; some commands have flags
that are multi-use and using multiple flags can be faster than
calling the same command multiple times with one flag.
This script is a little too long to paste in here, so download it here:
(right click, save Target As if it tries to open in the main window.
Eval Timer
This script makes a "hairball", it's just a curve with cv's at random positions
generated by sphrand.  But one of them assembles a command that creates the curve
by have multiple -p flags for each point.  The other one creates the curve first,
then appends the points to it one by one.  Each method also uses timerX to report
how long it took and there's a third procedure in there that executes them both
ten times each and averages the times taken.
In this case, the time differences aren't huge, but they are significant.
I remember another case where using eval to create one big command with multi-use flags
instead of calling the command many times made a drastic time difference.  It went from
a minute to a couple of seconds.  I really can't remember what it was though, so sorry
for bringing it up.

By the way, if you're running Maya 6.0, it's pretty fun to take one of those balls
and make it into a dynamic curve. Squiggly.

Here's a second example that's somewhat more useful:
This script is also too big to paste here, so download it to follow along.
(right click, save Target As if it tries to open in the main window.
Typeless Access to UI Elements.

That script allows you to get the value from all of the common UI elements
without worrying about what type they are.
The main usage for eval in this case was that once the script determined what type of control
an element was, it could generate the appropriate command to obtain the value.
At the end, eval is run with the command generated and the results are returned.
Here's the main advantage gained.  Once a type is determined, IE intFieldGrp,
the command is built something like this:
pseudo-code:

$command = ($controlType + " the appropriate flags " + $controlName);

Quite a few controls use the same flags:
intField, floatField, floatSlider, etc etc all use 
"typename -q -v"
Using eval this way saves at least 10-20 lines of code, because the other
option would have been to do an additional check of type for each variety
of control and have have the query command all ready to go for each type.

Dynamically assembling the command saves some typing, and skipping the extra conditionals
may add a fractional speed boost.
Any speed gain is probably tiny and inconsequencial, but it's nice to think about.

Here's a cute little calculator:
(right click, save Target As if it tries to open in the main window.
Cutest Calculator Ever

Eval is used here to obtain the results of the expression given.  
Two methods are shown for obtaining the results.  The first results in the creation of a
global variable.  The second doesn't, but is a litle bit more work to type.

It would even be possible to avoid global procedures altogether, by assembling a command
inside a string that would then be assigned to the textField to execute.  But that would be a
little bit of a hassle, what with all the escape sequences so that method isn't shown.

That's it for these examples of eval, once you start using it it's easy to get carried away.