One of the most important techniques of structured programming is partitioning a program into subprograms that have well-defined interfaces and protected implementations. In this chapter, you learn how to do the following:
Work with interfaces and implementations
Modularize Lua scripts
Use Lua modules from your application scripts
As a working example, you'll explore a Lua value inspection module that should prove useful when you develop Lua programs.
An interface is the part of a subprogram that is known to callers of the subprogram. The implementation refers to the way the subprogram works internally. If a subprogram's interface is clearly defined and its implementation doesn't depend on variables that can be modified by other program parts, then it has the desirable attribute of being reusable. This discipline is often referred to as encapsulation or information hiding or modularization. You've seen already how functions in Lua present an interface behind which local variables are protected from tampering. A function is loosely coupled (and therefore is more easily used in a variety of contexts) to the extent that it minimizes the scope of the variables it uses.
These notions of interface and implementation extend to files containing Lua code. When such a file is loaded, Lua converts it into a function, to which arguments can be passed and from which results can be returned. In this way, a Lua file has its own interface and its ...