You can also make modules containing procedure definitions and some global variables. These are called code modules and can be extremely useful. This section briefly outlines their construction and use. For in-depth details see the Reference Manual.
Code modules can be made by using the E compiler as you would to make an executable, except you put the statement OPT MODULE
at the start of the code.
Also, any definitions that are to be accessed from outside the module need to be marked with the EXPORT
keyword.
Alternatively, all definitions can be exported using OPT EXPORT
at the start of the code.
You include the definitions from this module in your program using MODULE
in the normal way.
The following code is an example of a small module:
OPT MODULE EXPORT CONST MAX_LEN=20 EXPORT OBJECT fullname firstname, surname ENDOBJECT EXPORT PROC printname(p:PTR TO fullname) IF short(p.surname) WriteF('Hello, \s \s\n', p.firstname, p.surname) ELSE WriteF('Gosh, you have a long name\n') ENDIF ENDPROC PROC short(s) RETURN StrLen(s)<MAX_LEN ENDPROC
Everything is exported except the short
procedure.
Therefore, this can be accessed only in the module.
In fact, the printname
procedure uses it (rather artificially) to check the length of the surname
.
It's not of much use or interest apart from in the module, so that's why it isn't exported.
In effect, we've hidden the fact that printname
uses short
from the user of the module.
Assuming the above code was compiled to module `mymods/name', here's how it could be used:
MODULE 'mymods/name' PROC main() DEF fred:PTR TO fullname, bigname fred.firstname:='Fred' fred.surname:='Flintstone' printname(fred) bigname:=['Peter', 'Extremelybiglongprehistoricname']:fullname printname(bigname) ENDPROC
Global variables in a module are a bit more problematic than the other kinds of definitions.
You cannot initialise them in the declaration or make them reserve chunks memory.
So you can't have ARRAY
, OBJECT
, STRING
or LIST
declarations.
However, you can have pointers so this isn't a big problem.
The reason for this limitation is that exported global variables with the same name in a module and the main program are taken to be the same variable, and the values are shared.
So you can have an array declaration in the main program:
DEF a[80]:ARRAY OF INT
and the appropriate pointer declaration in the module:
EXPORT DEF a:PTR TO INT
The array from the main program can then be accessed in the module! For this reason you also need to be pretty careful about the names of your exported variables so you don't get unwanted sharing. Global variables which are not exported are private to the module, so will not clash with variables in the main program or other modules.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.