Core Function Eval

From Sputnik Wiki
Revision as of 00:17, 18 November 2011 by UberFoX (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Eval( <expression>, <flag> )

Contents

Description

Evaluate a string as Sputnik code.

expression

Evaluates the string as Sputnik code. Among other things, this can be useful for storing code in a database text field for later execution.

There are some factors to keep in mind when using eval(). Remember that the string passed must be valid Sputnik code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things.

Also remember that variables given values under eval() will retain these values in the main script afterwards (Unless you choose to set the Eval() to have its own scope see the FLAG param).

flag

The scope flag options are :

0 = The Eval() code gets executed as if it was physical code in the current scope this will cause local variables in the Eval() to be available in the code that called it.

1 = The Eval() code gets executed in its own scope so local variables you create in the eval do not exist outside the eval, However you can modify global variables.

Return Value

None.

Remarks

N/A

Example

This runs in our scope so $Val will indeed be changed after the eval is over:

my $Val = 777;
println("Val is : " . $Val);
$a = eval( '$Val = 1221;' );
println("Val is : " . $Val);
println("Eval returned : " . $a);

This runs the eval in its own scope so it changing the $Val does not effect our local $Val

my $Val = 777;
println("Val is : " . $Val);
$a = eval( '$Val = 1221;' );
println("Val is : " . $Val);
println("Eval returned : " . $a);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox