What is an include?
It’s quite simply a file containing Mwnci code with functions and/or variable definitions that may be useful in other scripts
Creating an include
To create an include just save the code you want in a file with the file extension .mwnci
Save the below code as greeting.mwnci
function hello(Name) { println("Hello ", Name) }
Using the include
Now we can use the include we just created, by using the include command:
include("greeting") hello("Nobby") Hello Nobby
Contents of an include
Includes can contain functions, as already described, but also variables of all types (arrays, hashes) etc.
If we add this to the file greeting.mwnci
MyHash={ "Age": 45, "Toes": 10 }
And if we run:
include("greeting") hello("Nobby") println("You have ", MyHash["Toes"], " toes" Hello Nobby You have 10 toes
There are several built-in includes, which can be used at any time.
The files are searched via a ‘SearchPath’ which is controlled by the environment variable INCLUDEPATH. The default path is configured prior to compiling mwnci, such as /usr/local/include/mwnci. The current directory is searched after /usr/local/include/mwnci.
INCLUDEPATH is a colon (:) separated path that is searched prior to the default path.
if INCLUDEPATH is defined as “/home/detritus/include”, then at run time for the mwnci script it is set to:
/home/detritus/include:/usr/local/include/mwnci:.
You can also import multiple files:
include( "foo", "bar", "wibble" )
Or if you prefer:
include("foo") include("bar") include("wibble")
For ‘foo’ to be included, the file foo.mwnci must reside in /home/detritus/include, or /usr/local/include/mwnci or your current directory.
If you have files of the same name in more than one directory, then only the first found will be used. The file main.mwnci is auto included at script run time.