In contrast to included modules, true modules have some distinct properties that are shown in the list below.
Definition-time bindings
Definitions are evaluated in the sequence of the program flow.
One-time inclusion
In contrast to include
the
import
statement includes the module only once
even if there are more than one import
statement in the program flow. Later imports are silently ignored.
Proprietary global namespace
The module definition implies a module declaration that determines the namespace of the module's global variable scope.
Local environment
Aside from the data located in the module's global namespace all other data defined in the module is purely local, i.e. is invisible from the outside.
Module constructor function
Each true module may have a constructor function that is automatically executed upon first import.
The following listing is a brief sample of a true module.
{ // This is a module called "Sample". // Therefore the file name MUST be Sample.ycp // The "module" statement makes the module accessible for 'import'. // module "Sample"; // This is a local declaration. // It can only be 'seen' inside the module. // integer local_var = 42; // This is a global declaration. // It can be accessed from outside with the name space identifier 'Sample::'. // global integer global_var = 27; // This is a global function. // It has access to global_var *and* local_var. // global define sample_f () ``{ return local_var + global_var; } }
The module above can be used with the import
statement. The syntax for file inclusion with
import
is similar to include
.
The interpreter automatically appends “.ycp” to the
filename and searches below
/usr/lib/YaST2/modules
. If the filename starts
with “./”, the file is loaded from the local directory.
The global declarations of the module can then be accessed with the
name space identifier Sample::
.
![]() | Note |
---|---|
The file name must match the module declaration! Inside modules, only variable or function declarations are allowed. Stand-alone blocks or any kind of evaluation statements are forbidden. |
{ // This imports the 'Sample'-module. // import "Sample"; // The global function is called with the respective name space identifier. // integer i = Sample::sample_f(); // == 69 // No access to local module variables. // i = Sample::local_var; // ERROR, no access possible ! // No problem with global variables. // i = Sample::global_var; // == 27 Sample::global_var = 0; // This variable is writable !! return Sample::sample_f(); // == 42, since global_var is 0 }
![]() | Note |
---|---|
The first encounter of the statement |