Further clarification of namespaces.
1 parent 6b37ec5 commit f063bb55d153b64e291f8154bd85c1d76ed7e112
@tundra tundra authored on 14 Apr 2004
Showing 1 changed file
View
144
tconfpy.3
\'LegalVals\' that is, say, lower than allowed by
\'Min\' you will always get an error when the user sets
the variable to that value: It passed the \'LegalVals\'
validation but failed it for \'Min\'.
 
 
.SS Some Notes On Boolean Variables
 
One last note here concerns Boolean variables. Booleans are actually
stored in the symbol table as the Python values, True or False.
However, \*(TC accepts user statements that set the value of the
boolean in a number of formats:
 
.nf
 
Boolean True Boolean False
------------ -------------
 
foo = 1 foo = 0
foo = True foo = False
foo = Yes foo = No
foo = On foo = Off
 
.fi
 
This is the one case where \*(TC is insensitive to case -
"tRUE", "TRUE", and "true" are all accepted, for example.
 
.B NOTE HOWEVER:
If the user wants to do a conditional test on the value of
a boolean they
.B must
observe case and test for either \'True\' or \'False\':
 
.nf
 
.if [boolvar] != False # This works fine
 
.if [boolvar] != FALSE # This does not work - Case is not being observed
 
.if [boolvar] != Off # Neither does this - Only True and False can be tested
 
.fi
 
 
.SS The Initial Symbol Table And Lexical Namespaces
 
.fi
 
.IP \(bu 4
Case is always significant except when assigning a value to Booleans
(described in the previous section).
(described in the section below entitled,
.B Some Notes On Boolean Variables
).
 
.IP \(bu 4
Regardless of a variable's type, all variable references
return
House Cleaning = Dad
.fi
 
This is illegal because whitespace is not permitted in variable names.
\*(TC will produce an error if it sees such a construct.
\*(TC will produce an error if it sees such a construct. As a general
matter, any variable you construct through this indirection method
must still conform to all the rules of variable naming: It cannot
contain whitespace, begin with \'$\', contain \'#\', \'[\', or \']\' and
so on.
 
Get into the habit of reading \'[something]\' as, "The current value
of \'something\'". See if you understand what the following does (if
you don't, try it out with \'test-tc.py\'):
This just set the value of the variable \'Namespace\' to
\'something\' and has nothing whatsoever to do with lexical
namespaces.
 
This all works, by the way, because \'NAMESPACE\' is
nothing more than (yet) another variable in the symbol table. \*(TC
just understands that variable to be special - it treats it as the
repository of the current lexical namespace.
 
 
.IP \(bu 4
 
Whichever method you use to change it,
.B the new namespace cannot begin with \'$\'.
Both of the following will cause an error:
.B the new namespace must follow all the same rules used for naming variables.
 
For example, both of the following will cause an error:
 
.nf
[$FOO]
OR
above entitled,
.B The Initial Symbol Table And Lexical Namespaces
for details.)
 
.IP \(bu 4
Lexical namspaces are implemented by having \'NAMESPACE\' just be
nothing more than (yet) another variable in the symbol table. \*(TC
just understands that variable to be special - it treats it as the
repository of the current lexical namespace. This means you can use
the value of NAMESPACE in your own string substitutions:
 
.nf
MyVar = [NAMESPACE]-Isn't This Cool?
.fi
 
You can even use the current value of NAMESPACE when setting a new
namespace:
 
 
.nf
NAMESPACE = [NAMESPACE]-New
.fi
 
One final, but very important point is worth noting here. The
\'NAMESPACE\' variable itself is always understood to be
.B relative to the root namespace.
No matter what the current namespace actually is, \'[NAMESPACE]\' or
\'NAMESPACE = ...\' always set a variable by that name in the
root namespace. Similarly, when we use a variable reference to get
the current namespace value (as we did in the example above), \'NAMESPACE\'
is understood to be relative to the root namespace. That's why things
like this work:
 
.nf
[MyNewSpace]
x = 100 # MyNewSpace.x = 100
y = [NAMESPACE]-1 # MyNewSpace.y = MyNewSpace-1
NAMESPACE = NewSpace # .NAMESPACE = NewSpace
fi
 
.SS Pre-Defined Variables
 
.SS The \'.include\' Directive
 
.SS \'.literal\. - Using \*(TC As A Preprocessor For Other Languages
 
.SS Type And Value Enforcement
 
.SS Some Notes On Boolean Variables
 
One last note here concerns Boolean variables. Booleans are actually
stored in the symbol table as the Python values, True or False.
However, \*(TC accepts user statements that set the value of the
boolean in a number of formats:
 
.nf
 
Boolean True Boolean False
------------ -------------
 
foo = 1 foo = 0
foo = True foo = False
foo = Yes foo = No
foo = On foo = Off
 
.fi
 
This is the one case where \*(TC is insensitive to case -
"tRUE", "TRUE", and "true" are all accepted, for example.
 
.B NOTE HOWEVER:
If the user wants to do a conditional test on the value of
a boolean they
.B must
observe case and test for either \'True\' or \'False\':
 
.nf
boolvar = YES
 
.if [boolvar] != False # This works fine
 
.if [boolvar] != FALSE # This does not work - Case is not being observed
 
.if [boolvar] != Off # Neither does this - Only True and False can be tested
 
.fi
 
 
.SH ADVANCED TOPICS
 
.SS Guaranteeing A Correct Base Configuration