Using Nasal functions: Difference between revisions

Jump to navigation Jump to search
m
(→‎Function closures: new section)
Line 384: Line 384:


Whenever you have very similar lines of code that seem fairly repetitive, it is a good idea to consider introducing small helper functions. You can use plenty of small helper functions and then just "chain" them together, rather than using complex nested expressions that make your head spin.
Whenever you have very similar lines of code that seem fairly repetitive, it is a good idea to consider introducing small helper functions. You can use plenty of small helper functions and then just "chain" them together, rather than using complex nested expressions that make your head spin.
== Another way to call functions ==
... using '''call()'''. Sometimes, you may need to more finegrained control over functions that you want to call, such as specifying an object for a method call, or for exception handling purposes. This is accomplished using Nasal's '''call()''' API:
<syntaxhighlight lang="php">
call(function, vector_of_arguments, object, namespace, error_vector);
</syntaxhighlight>
This will call the given function with the given arguments in the vector and returns the result. The only required argument is the function (and vector of arguments respectively), all other arguments are optional and defaulted to nil.
The optional arguments can be used to specify the "me" reference for a function call (object) and the local variable namespace. The error argument, if present, must be a vector. If the called function terminates due to a runtime error (exception) or die() call, the error (either a string or the argument to die() is appended to the vector, followed by the file name at which the error occurred, followed by the line number in that file, followed in order by each file/line number on the call stack.
To learn more, see [http://plausible.org/nasal/lib.html].

Navigation menu