Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#139 closed proposed feature (fixed)

variables that live and are valid in a lib (with default value)

Reported by: seelisch Owned by: hannes
Priority: minor Milestone: 3-1-1
Component: singular-libs Version: 3-1-0
Keywords: lib variable Cc:

Description

suggestion by Winfried Bruns - to be evaluated first

He wrote: "Es wäre sehr nützlich, wenn man innerhalb einer Bibliothek Variablen deklarieren und mit Anfangswerten versehen könnte, die für diese Bibliothek global sind. Angesichts des Package-Konzeptes sollte das kein Problem sein. Aber soweit ich das herausfinden konnte, geht es einfach nicht---oder nur so: Man deklariert die Variablen innerhalb einer Funktion der Bibliothek und exportiert sie dann.

Das ist ja eine Möglichkeit, aber man weiß nie, ob die Variable schon deklariert ist, wenn man sie abfragen oder setzen will. Ich habe in unserer Bibliothek das Problem mit einem Trick umgangen, der aber letzten Endes fragwürdig ist."

Change History (3)

comment:1 Changed 14 years ago by seelisch

Milestone: Releases 3-1-1 and higher

comment:2 Changed 12 years ago by hannes

Resolution: fixed
Status: newclosed

Global variables in a library can be constructed in a procedure mod_init and exported. This procedure will be automatically called during the loading of the library. Example:

proc mod_init()
{
  int global_i;
  export global_i;
}

comment:3 Changed 12 years ago by gorzel

1.) Why not give it the name lib_init instead of mod_init ?

2.) It seems that this mechanism is not yet documented in the manual.

Note / mention that the purpose is not limited for declaring global variables but executing any command at loading.

3.) concerning Bruns's request:

"man weiß nie, ob die Variable schon deklariert ist, wenn man sie abfragen oder setzen will." Translation:

"One never knows whether the variable is already declared, if one wants to call or read it."

I don't think so:

The return value of defined(Packagename::varname)

shows whether and where the variable is declared.

defined( ) ==0 i.e. not defined

defined( ) ==1 defined as a global variable

defined( ) > 1 defined as a local variable

Note: See TracTickets for help on using tickets.