Core Function Eval

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(flag)
(Example)
Line 114: Line 114:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Notice the @"" lets you type strings on multiple lines where as a regular "" does not also note @"" ignores escapes such as \n and if you wish to place a " inside the string you must define it as "".
+
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)
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$val = "Hello";
 +
say eval('strlen($val) == 5 && $val[0] == "H"', 3);
 +
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 10:01, 7 September 2013

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. 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".

Return Value

Whatever is returned by the evaluated code.

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);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox