# HG changeset patch # User jbe # Date 1503319827 -7200 # Node ID 02d505b6090c90c47fac47650bcafe5698cac6c1 # Parent 39ee5d9a90a05f5bf12144c1e8a5ed2f852d6801 Added section about global variables and the environment to WebMCP's documentation diff -r 39ee5d9a90a0 -r 02d505b6090c doc/autodoc-header.htmlpart --- a/doc/autodoc-header.htmlpart Mon Aug 21 14:37:35 2017 +0200 +++ b/doc/autodoc-header.htmlpart Mon Aug 21 14:50:27 2017 +0200 @@ -304,6 +304,16 @@

Such translation files can be automatically created with the langtool.lua program, found in the framework's bin/ directory.

+

Global variables and the environment

+

+ To avoid accidental programming errors, WebMCP forbids setting of global variables by default. This is overridden by using the prefix "_G." (see reference) when setting the variable, e.g. _G.myvar = 7, or by setting the variable in a file with the same name of the global varaible (but suffixed with ".lua") in the env/ directory of the framework or application. Note, however, that the lifetime of global variables is not predictable as it depends on process recycling of the Moonbridge webserver (one fork will handle more than one request) and because there may be multiple forks of the Lua machine which all have their own global variable space (there is usually more than one fork). +

+

+ Global variables are still useful when providing access to libraries, for example. WebMCP automatically loads certain libraries and functions though an autoloader mechanism. On read-accessing any unknown variable, WebMCP will search the framework's and application's env/ directories for an appropriate file (e.g. "my_func.lua if you invoke "my_func()") or a matching directory (e.g. "my_module/ if you access "my_module.my_func()). In case of an existing directory in env/, an empty table with autoloading capabilities is automatically created as global variable with the name of the directory. The autoloading mechanism allows directories to contain further files which can be used to initialize variables within that table when accessed. Directories can also contain a special file called "__init.lua" that always gets executed when the table is accessed for the first time. The env/ root directory can also contain a file ("env/__init__.lua") which gets executed before any configuration is loaded. +

+

+ If an application needs to store request related data, the global table app should be used. This table gets automatically initialized (i.e. emptied) for each request. +

Directory structure of a WebMCP application

Summarizing information from the previous section, we get the following directory structure for a WebMCP application: @@ -372,6 +382,23 @@

  • + env/ + +
  • locale/ (translation files used by the underscore function")