Changing The Namespace (The GNU Awk User’s Guide)
Next: Naming Rules, Previous: Default Namespace, Up: Namespaces [Contents][Index]
15.4 Changing The Namespace
In order to set the current namespace, use an @namespace directive at the top level of your program:
@namespace "passwd"
BEGIN { … }
…
After this directive, all simple non-completely-uppercase identifiers are placed into the passwd namespace.
You can change the namespace multiple times within a single source file, although this is likely to become confusing if you do it too much.
NOTE: Association of unqualified identifiers to a namespace is handled while
gawkparses your program, before it starts to run. There is no concept of a “current” namespace once your program starts executing. Be sure you understand this.
Each source file for -i and -f starts out with an implicit ‘@namespace "awk"’. Similarly, each chunk of command-line code supplied with -e has such an implicit initial statement (see section Command-Line Options).
Files included with @include (see section Including Other Files into Your Program) “push” and “pop” the current namespace. That is, each @include saves the current namespace and starts over with an implicit ‘@namespace "awk"’ which remains in effect until an explicit @namespace directive is seen. When gawk finishes processing the included file, the saved namespace is restored and processing continues where it left off in the original file.
The use of @namespace has no influence upon the order of execution of BEGIN, BEGINFILE, END, and ENDFILE rules.
Next: Naming Rules, Previous: Default Namespace, Up: Namespaces [Contents][Index]