Well, it's a bit misleading to say that module variables are generally assumed to be constants at compile time. They are variables, but the '.' operator is evaluated at compile time, so anything indexed through it becomes a constant value.
As for imports, any identifier it adds are accessed as through a '.' operator, so they become constants too.
/.../ If you really want to do this (and have a global object), you need a query method to get the global object every time.
No, it's enough to index the module with '->' instead. Al's example with a module and a variable inside it with the same name makes it unnecessarily confusing, so I prove another example:
Foo.pmod:
int var = 17;
foo.pike:
import "."; int main () { werror ("%O\n", Foo.var); // writes 17 Foo->var = 42; werror ("%O\n", Foo.var); // still writes 17 werror ("%O\n", Foo->var); // writes 42 }
(I import '.' instead of '.Foo' to more clearly show the relation between '.' and '->'.)
/ Martin Stjernholm, Roxen IS
Previous text:
2004-02-02 10:37: Subject: Re: Pike module initialization at compile time
OK, then it leads to another question - if modules are objects, why I can't modify variables, defined in those modules (directly)? And what _exactly_ happening when I import or inherit the module (the docs are a bit short in this topic)?
I'm not really sure. ;)
I don't know if there is a difference between inheriting objects and inheriting the object_program(object). Ie, you will inherit the objects program and thus get your own set of variables.
Module variables can be modified, but are assumed to be constants at compile-time. Ie,
int x=17; ... x=42; // at a later stage
yourmodule.x will here be 17 in some compilations and 42 in later. And never change where you use it outside the module.
int main() { Global_init(); write("Global's value: %O\n", Global->method()); }
See above. Modules are just there to give namespace. If you really want to do this (and have a global object), you need a query method to get the global object every time.
Ie,
object TheGlobal() { return Global; } ... write("Global's value: %O\n", TheGlobal()->method());
There is no use in accessing variables in modules from outside the module.
You *can* however instantiate the Global object when the module is created, like this:
object Global=GlobalClass();
and use it as a *constant*.
/ Mirar