awk
’s Single Namespace ¶In standard awk
, there is a single, global, namespace.
This means that all function names and global variable names must
be unique. For example, two different awk
source files cannot
both define a function named min()
, or define the same identifier,
used as a scalar in one and as an array in the other.
This situation is okay when programs are small, say a few hundred
lines, or even a few thousand, but it prevents the development of
reusable libraries of awk
functions, and can inadvertently
cause independently-developed library files to accidentally step on each
other’s “private” global variables
(see Naming Library Function Global Variables).
Most other programming languages solve this issue by providing some kind of namespace control: a way to say “this function is in namespace xxx, and that function is in namespace yyy.” (Of course, there is then still a single namespace for the namespaces, but the hope is that there are much fewer namespaces in use by any given program, and thus much less chance for collisions.) These facilities are sometimes referred to as packages or modules.
Starting with version 5.0, gawk
provides a
simple mechanism to put functions and global variables into separate namespaces.