Modules

If the PACKAGE statement is omitted, one or more external procedures can exist in a single Open PL/I source file as shown in this code sample.

PKG: PACKAGE EXPORTS (SUB1 EXTERNAL(‘PLB’));
   SUB1: PROC;  /* will be external, callable as “PLB”) */
       CALL SUB2();
   END SUB1;

   SUB2: PROC;  /* will be internal, not exported */
       PUT SKIP LIST (‘PACKAGE NAME = ‘, PACKAGENAME());
   END SUB2;

END PKG;

All external procedures within a source file are compiled into a single object file.

A module is defined as a single compilation unit, a source file that is compiled into object code. A module consists of one or more external procedures.

A source file may contain declarations that appear outside the scope of any external procedure, as long as all variables that appear in such declarations are BASED, STATIC, or DEFINED. A variable that is defined this way is known within the scope of all external procedures. If a declaration is provided with the EXTERNAL attribute, it will be known outside the source file.

A source file may also contain %REPLACE statements and declarations of named constants (for example, file and entry constants) before the first external procedure.

The following example illustrates a source file that has declarations and %REPLACE statements preceding the first external procedure. There are two procedures (GETREC and PUTREC) in the source file that can be used as part of the program. By default, Open PL/I declares these entry constants, GETREC and PUTREC, with the EXTERNAL attribute; therefore, a procedure from another module outside of this source file can invoke GETREC or PUTREC. If a procedure in another module needs to reference GETREC and PUTREC, they must be declared within that module with the attributes ENTRY and EXTERNAL.

At the top of this source file are other items (REC, NAME, ADDRESS) that are hidden from any procedures compiled in other modules, and are known only to GETREC and PUTREC. Because Open PL/I, by default, provides file constant declarations with the EXTERNAL attribute, the file DATA_BASE can be known in other modules.

%REPLACE DATA_SIZE BY 80;
DECLARE 1 REC BASED
            2 NAME CHAR(40),
            2 ADDRESS CHAR(DATA_SIZE); 
DECLARE DATA_BASE KEYED FILE UPDATE;

GETREC: PROCEDURE(P);
DECLARE P POINTER;
READ FILE(DATA_BASE) INTO(P->REC); 
END GETREC;

PUTREC: PROCEDURE(P); 
   DECLARE P POINTER; 
    REWRITE FILE(DATA_BASE) FROM(P->REC);
END PUTREC;