Compiling and using a Library

Since this is a function that could be used by other programs that reference the customer table, the function will be compiled into a library. The library can then be linked into any program, and the function called.

The function will always return store_num and store_name. If the FOREACH fails, or returns no rows, the calling program will have a store_num of zero and a NULL store_name returned.

The function is contained in a file named cust_lib.4gl. This file would usually contain additional library functions. To compile (and link, if there were additional .4gl files to be included in the library):
fgl2p -o cust_lib.42x cust_lib.4gl

Since a library has no MAIN function, we will need to create a small stub program if we want to test the library function independently. This program contains the minimal functionality to test the function.

Example: cust_stub.4gl

The module cust_stub.4gl calls the library function display_custarr in cust_lib.4gl.

Module cust_stub.4gl:
01 SCHEMA custdemo 
02
03 MAIN
04   DEFINE store_num LIKE customer.store_num,
05         store_name LIKE customer.store_name 
06
07  DEFER INTERRUPT
08  CONNECT TO "custdemo"
09  CLOSE WINDOW SCREEN
10
11  CALL display_custarr() 
12           RETURNING store_num, store_name 
13  DISPLAY store_num, store_name 
14
15  DISCONNECT CURRENT
16
17 END MAIN
Note:
  • Lines 04 and 05 define variables to hold the values returned by the display_custarr function.
  • Lines 07 thru 09 are required simply for the test program, to set the program up and connect to the database.
  • Line 11 calls the library function display_custarr.
  • Line 13 displays the returned values to standard output for the purposes of the test.

Now we can compile the form file and the test program, and link the library, and then test to see if it works properly.

fglform manycust.per
fgl2p -o test.42r cust_stub.4gl cust_lib.42x
fglrun test.42r