Core Function Eval

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
Line 95: Line 95:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Heres a cool example of using the qq~~; features :
+
Heres a cool example of using the @"" features :
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">

Revision as of 13:11, 3 December 2011

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

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 where as a regular "" does not.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox