process. Compiling and processing string

^process{string}
^process[scope]{string}
^process[scope]{string}
[options]

String will be compiled and executed as code in Parser, within specified scope or current scope. Specified scope can be an object or a class, but not method (this meaning if you process something inside your method, the method`s local varuables will not available inside processed code).

This operator is useful when you need to store fragments of code or your own methods in files with extension other than
.html-and which therefore will not be processed by Parser by default-or in a DB.

Several
options (hash) may be specified:
·$.main[a new name for main method, declared in code in string]  
·$.file[a name of file, from which this string comes from]  
·$.lineno(a line number in file, where this string cames from. may be negative)  
·$.replace(true)-starting from version 3.4.3 the exception occurs in case of loading class if a class with the same name was already loaded. It can be switched off by specifying a new $.replace(true) option.  

Simple examples
^process{@extra[]
   
PS: you look really good…

}
Method extra will be added to the current class and you will be able to call it later on.

^process[$engine:
CLASS]{@start[]
   
5… 4… 3… 2… 1… GO!

}
Method start will be added to user class engine.

$running_man[^man
::create[Jack]]
^process[$running_man]{
    
Name$name<br />
}
As the code is executed within the scope of object $running_man, it is able to use the object's field name and output "Jack".

Include operator
@include[filename][file]
$file[^
file::load[text;$filename]]
^process[$
caller.self]{^taint[as-is][$file.text]}[
   $.file[$filename]
]
The code loads specified file and executes it within the scope of the current object/class when include was called. File option allows us to specify the name of file, where this code were loaded from. In case there would be some error, you would see this "file name".
Note: "scope of current call" does not include any local variables or parameters!

Complex example
It is often convinient to compile a code to some method, which name evaluated dynamically:
# this is source code, note ^^
$source_code[
2*2=^^eval(2*2)]
it is evaluated dynamically, that we need to create the "method1" method 
$method_name[method1]
compiling source code, storing it to new method
^process{$source_code}[
   $.main[$method_name]
]

later in code it can be called
^method1[]
This example would continue to work even if in $source_code there would be declared several methods, because main option sets the name of main method.


Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 22.10.2013