ファイルを渡す方法および関連付けられている変数を含むデータ構造を渡す方法の例

関連付けられた変数を保持するためのデータ構造をファイルに定義する方法、ファイルおよびデータ構造をパラメーターとしてプロシージャーに渡す方法、ならびにプロシージャー内のパラメーターの使用方法を、以下の例で説明します。

図 1. /COPY ファイル INFILE_DEFS
 * The /COPY file has template definitions for the File and Associated Variables

 /if defined(FILE_DEFINITIONS)
 // Template for the "INFILE" file type
Finfile_t  if   e             disk    template block(*yes)
F                                     extdesc('MYLIB/MYFILE')
 /eof
 /endif
 /if defined(DATA_DEFINITIONS)
 // Template for the associated variables for an INFILE file
D infileVars_t    ds                  qualified template
D    filename                   21a
D    mbrname                    10a
 // Prototype for a procedure to open an INFILE file
D open_infile     pr
D    theFile                          likefile(infile_t)
D    kwVars                           likeds(infileVars)
D                                     options(*nullind)
 /eof
 /endif
図 2. ファイル・パラメーターを渡す呼び出し元プロシージャー
P myproc          b // Copy in the template and prototype definitions
 /define FILE_DEFINITIONS
 /COPY INFILE_DEFS
 /undefine FILE_DEFINITIONS

 /define DATA_DEFINITIONS
 /COPY INFILE_DEFS
 /undefine DATA_DEFINITIONS
 // Define the file using LIKEFILE, to enable it to be passed as
 // a parameter to the "open_infile" procedure.

 // Define all the associated variables as subfields of a data
 // structure, so that all the associated variables can be
 // passed to the procedure as a single parameter
Ffile1                                likefile(infile_t)
F                                     extfile(file1Vars.filename)
F                                     extmbr(file1Vars.mbrname)
F                                     usropn

D file1Vars       ds                  likeds(infileVars_t)

  /free
         open_infile (file1 : file1Vars);

         . . .
図 3. ファイル・パラメーターを使用する呼び出し先プロシージャー
 // Copy in the template and prototype definitions
 /define FILE_DEFINITIONS
 /COPY INFILE_DEFS
 /undefine FILE_DEFINITIONS

 /define DATA_DEFINITIONS
 /COPY INFILE_DEFS
 /undefine DATA_DEFINITIONS
P open_infile     b
 // The open_infile procedure has two parameters
 //  - a file
 //  - a data structure containing all the associated variables for the file
D open_infile     pi
D    theFile                          likefile(infile_t)
D    kwVars                           likeds(infileVars)
  /free
         // The %OPEN(filename) built-in function reflects the
         // current state of the file
         if not %open(theFile);
            // The called procedure modifies the calling procedure's "file1Vars"
            // variables directly, through the passed parameter
            kwVars.extfile = 'LIB1/FILE1';
            kwVars.extmbr = 'MBR1';
            // The OPEN operation uses the file1Vars subfields in the
            // calling procedure to open the file, opening file LIB1/FILE1(MBR1)
            open theFile;
         endif;
         . . .