Core Function Eval

From Sputnik Wiki
Jump to: navigation, search
Eval( <expression>, <flag/eval>, <cleanup>, <printToReturn> )

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/eval

Optional; Either an Eval object created from EvalCreate() or a scope flag

If an Eval object is given then it will work like Flag 4

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. Any Return statement will return a value back to the Eval() caller.

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. Any Return statement will return a value back to the Eval() caller.

2 = The Eval() code gets executed same as a Require() meaning its functions/classes etc get added to main code but no statements are executed such as println().

3 = The Eval() code gets executed as an expression and its return value is given as the return to Eval() (This works very similar to flag 0 however it is only for a single expression such as: "strlen($val) < 3 && 1 == $val2".

4 = The Eval() code gets executed in a separate Sputnik instance (The instances are created on a per-thread basis so each unique thread you are using will have its own Sputnik instance for Eval() created if Eval() with flag 4 is used).

Default: 0

cleanup

Optional; Delete all functions/classes etc inserted by the Eval() statement after the Eval() ends.

This will be ignored if the flag is set to 3.

Default: False.

printToReturn

Optional; If enabled all print,echo,say etc statements will print to the return value instead of the console.

Default: False.

Return Value

Success: Whatever is returned by the evaluated code.

Failure: Null.

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);

This example uses eval to run a function and return its value:

$a = eval( ' return Add(10, 20); ' );
println("Eval returned : " . $a);
 
Function Add($a, $b)
{
	return $a + $b;
}

Same as above but this time we set the variable names :

$val1 = 100;
$val2 = 60;
 
$a = eval( " return Add($val1, $val2); " );
println("Eval returned : " . $a);
 
Function Add($a, $b)
{
	return $a + $b;
}

Notice "" was used when the eval had $val1 etc int it? Thats because "" strings can contain escapes and special variables however "" strings are not good for evals since they get in the way of using " you have to use many \" which can look ugly.

In this example the function itself is also declared in the Eval :

$a = eval( 
			'return Add(44, 100);' .
			'Function Add($a, $b)' .
			'{' .
			'	return $a + $b;' .
			'}'
			);
println("Eval returned : " . $a );

Heres a cool example of using the @"" features :

$val = eval(
	@"
		return Add(100, 300);
		Function Add($a, $b)
		{
			return $a + $b;
		}
	"
);
println("Eval returned '$val'");

Notice the @"" lets you type strings on multiple lines same as a regular "" however note @"" ignores escapes and variables such as \n and $var etc and if you wish to place a " inside the string you must define it as "".

Example of flag 3 (Notice ; is not required here)

$val = "Hello";
say eval('strlen($val) == 5 && $val[0] == "H"', 3);

Using just a string to Eval stuff

// As this know this will return 200
$a = eval( 'return 100 * 2;' );
// However using { } curly braces you can do same thing as an eval
// Example
echo "Value is {return 100 * 2} ok";
// Prints: Value is 200 ok
// In a way its similar to an Eval since it does run Sputnik code however
// the return is placed directly into the string
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox