|
By Andrew J. Wozniewicz
Milwaukee, August 16, 2008
To "invoke" a module is to "call" it like a procedure or function. In WANTScript, all modules are callable subroutines that can be invoked by referring to their name whenever a statement is allowed. For example:
module TestProcs
Test1
Test2
module Test1
WriteLn("Test1 called");
end
module Test2
WriteLn("Test2 called");
end
end
The two nested modules, named Test1 and Test2, are invoked inside the module TestProcs, just like procedures would be. In fact, given that you can use the keywords program and procedure as an alias for module in this context, the preceding example can be re-written in the following way, to make it more intuitive to a Pascal programmer:
program TestProcs;
procedure Test1;
WriteLn("Test1 called");
end;
procedure Test2;
WriteLn("Test2 called");
end;
Test1;
Test2;
end
The only things missing from the preceding example, that would make it a valid Pascal program, are the word "begin" in a few places, and the period at the end. The output of both versions of this program/module is – predictably – as follows:
Test1 called
Test2 called
Not-so-obvious to a Pascal programmer is the fact that one can also invoke the top-level module TestProcs just like a procedure. This is akin to calling a Pascal unit directly by name, which is not allowed in Pascal (the code of a unit – its initialization section – is automatically called by the runtime). Here, the module that was previously treated as a top-level program, is now used both like a subroutine and importable library:
program ExerciseTestProcs
import TestProcs
TestProcs
end
This program produces exactly the same output as before:
Test1 called
Test2 called
The difference with the previous example is that the module TestProcs is called as a procedure from within ExerciseTestProcs – it is no longer used as a top-level module, but as a callable subroutine.
In general, in WANTScript, any module can potentially be called as a subroutine. Calling a module as a subroutine runs its executable statements, if any.
A top-level module run from the command line is simply being called just like a subroutine by the WANT Runtime Engine.
-Andrew
|