- +++ Scripting: Now there are global variables.
By default, all variables in XY scripting are local, i.e. they are not shared between called and calling scripts. In other words, whenever one script calls another script (e.g. using commands "sub" or "load"), a new local namespace is created by the called script and pushed on the stack.
Generally, global variables are shared between scripts. However, the mechanism of "globalization" -- (almost) identical to the one used in PHP -- now added to scripting gives you maximum control over what is shared and where. It's implemented by means of a new command:
Name: Global
Action: Define one or more variables as global
Syntax: global variable(s)
variable: A single variable, or a comma-separated list of up to
10 variables. If a variable has already been defined as global
before, then the global command initializes it to its current
global value, else it is initialized to an empty string.
Example:
Here are 2 scripts where the first is calling the second:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"script1"
$foo = 4; // set local value: new local var $foo is created
msg $foo; // 4 (local)
// now make $foo global
// it's initialized to [empty] (assuming nothing created
// a global $foo before with a different value, which is
// possible when "script1" has been called by yet another
// script that has declared $foo as global)
global $foo;
msg $foo; // [empty] (global)
$foo = 8; // set global value
msg $foo; // 8 (global)
sub script2; // go down
msg $foo; // 16 (global, as set in script2)
msg $bar; // $bar (uninitialized, not a variable)
// make $bar global
// it's set to "tequila" (as previously set in script2)
global $bar;
msg $bar; // tequila
"script2"
msg $foo; // $foo (uninitialized, not a variable)
$foo = 15; // set local value: new local var $foo is created
msg $foo; // 15 (local)
// now make $foo global
// it's set to 8 (as previously set in script1)
// also create a new global $bar
global $foo, $bar;
msg $foo; // 8 (global)
$foo = 16; // set global value
$bar = "tequila"; // set global value
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Notes
- You can have as many global vars as you like, but it be no more
than 10 per "global" statement for internal reasons. IOW, add a
new "global" line for each pack of 10 variables if you really
need that many.
- It's recommended that you reflect the global nature of a variable
in the variable name, for example by prepending $g_ to global
vars. It will make your code easier to read and maintain. - * Scripting Step Mode: Improved handling of binary data. There were some display glitches with binary file data, and unnecessary slowness. Now you can make a pretty fast copy of the current file, even if the file is not small and with step mode enabled, using e.g. this:
::writefile("<curbase>-copy.<curext>",
readfile("<curname>", "b"), ,"b"); - * Scripting Step Mode: Variables on stack are now displayed with up to 256 chars (rest cropped). Non-printable chars are displayed as ".".
Monday, October 27, 2008
v7.70.0010 - 2008-10-27 14:38
Entry Type: Beta Update Posted by XYplorer user : J_C_Hallgren at 8:39 AM