In this module, we will explore how to call external programs from within a CL (Control Language) script. This is a crucial skill for integrating various systems and leveraging existing functionalities provided by other programs.
Objectives
By the end of this module, you will:
- Understand the concept of calling external programs.
- Learn the syntax and commands used to call external programs.
- Practice calling external programs with practical examples.
- Handle errors and return values from external programs.
- Introduction to Calling External Programs
Calling external programs allows a CL script to execute other programs, which can be written in different languages or be part of the system's utilities. This capability is essential for:
- Reusing existing code.
- Integrating different systems.
- Automating complex workflows.
- Basic Syntax
The primary command used to call an external program in CL is CALL
. The syntax is straightforward:
PGM(program_name)
: Specifies the name of the program to be called.PARM(parameter_list)
: Specifies the parameters to be passed to the program.
Example
Let's consider a simple example where we call a program named MYPGM
with two parameters:
- Practical Example
Example Program: MYPGM
First, let's create a simple program named MYPGM
that takes two parameters and prints them.
// MYPGM.RPGLE PGM PARM(&PARAM1 &PARAM2) DCL VAR(&PARAM1) TYPE(*CHAR) LEN(10) DCL VAR(&PARAM2) TYPE(*CHAR) LEN(10) SNDPGMMSG MSG('Parameter 1: ' *CAT &PARAM1) SNDPGMMSG MSG('Parameter 2: ' *CAT &PARAM2) ENDPGM
Calling MYPGM
from a CL Script
Now, let's write a CL script to call MYPGM
with parameters.
// CALLMYPGM.CLLE PGM DCL VAR(&PARM1) TYPE(*CHAR) LEN(10) VALUE('Hello') DCL VAR(&PARM2) TYPE(*CHAR) LEN(10) VALUE('World') CALL PGM(MYPGM) PARM(&PARM1 &PARM2) ENDPGM
Explanation
- Declare Variables: We declare two variables
&PARM1
and&PARM2
with initial values 'Hello' and 'World'. - Call Program: We use the
CALL
command to callMYPGM
and pass the variables as parameters.
- Handling Errors and Return Values
When calling external programs, it's essential to handle potential errors and capture return values. The MONMSG
command can be used to monitor for specific messages and handle them accordingly.
Example with Error Handling
// CALLMYPGM_WITH_ERROR_HANDLING.CLLE PGM DCL VAR(&PARM1) TYPE(*CHAR) LEN(10) VALUE('Hello') DCL VAR(&PARM2) TYPE(*CHAR) LEN(10) VALUE('World') CALL PGM(MYPGM) PARM(&PARM1 &PARM2) MONMSG MSGID(CPF0000) EXEC(DO) SNDPGMMSG MSG('Error occurred while calling MYPGM') ENDDO ENDPGM
Explanation
- Call Program: We call
MYPGM
as before. - Monitor for Errors: We use
MONMSG
to catch any error messages (CPF0000
is a generic message ID for all errors) and execute a block of code if an error occurs. - Send Error Message: If an error occurs, we send a program message indicating the error.
- Exercises
Exercise 1: Call an External Program
- Create a program named
EXTPGM
that takes one parameter and prints it. - Write a CL script to call
EXTPGM
with a parameter value of your choice.
Solution
EXTPGM
// EXTPGM.RPGLE PGM PARM(&PARAM) DCL VAR(&PARAM) TYPE(*CHAR) LEN(10) SNDPGMMSG MSG('Parameter: ' *CAT &PARAM) ENDPGM
CL Script
// CALLEXTPGM.CLLE PGM DCL VAR(&PARM) TYPE(*CHAR) LEN(10) VALUE('TestValue') CALL PGM(EXTPGM) PARM(&PARM) ENDPGM
Exercise 2: Error Handling
- Modify the
EXTPGM
program to intentionally cause an error (e.g., divide by zero). - Update the CL script to handle the error and print an appropriate message.
Solution
EXTPGM with Error
// EXTPGM.RPGLE PGM PARM(&PARAM) DCL VAR(&PARAM) TYPE(*CHAR) LEN(10) DCL VAR(&ZERO) TYPE(*DEC) LEN(3 0) VALUE(0) DCL VAR(&RESULT) TYPE(*DEC) LEN(3 0) CHGVAR VAR(&RESULT) VALUE(1 / &ZERO) // This will cause a divide by zero error SNDPGMMSG MSG('Parameter: ' *CAT &PARAM) ENDPGM
CL Script with Error Handling
// CALLEXTPGM_WITH_ERROR_HANDLING.CLLE PGM DCL VAR(&PARM) TYPE(*CHAR) LEN(10) VALUE('TestValue') CALL PGM(EXTPGM) PARM(&PARM) MONMSG MSGID(CPF0000) EXEC(DO) SNDPGMMSG MSG('Error occurred while calling EXTPGM') ENDDO ENDPGM
Conclusion
In this module, we learned how to call external programs from a CL script, pass parameters, and handle errors. This capability is vital for integrating different systems and reusing existing functionalities. In the next module, we will explore interfacing with databases, which will further enhance our ability to create powerful and integrated CL scripts.
CL (Control Language) Course
Module 1: Introduction to CL
- What is Control Language?
- Setting Up Your Environment
- Basic Syntax and Structure
- Writing Your First CL Program
Module 2: Basic CL Commands
- Introduction to CL Commands
- File Management Commands
- Job Management Commands
- System Management Commands
Module 3: Variables and Expressions
Module 4: Control Structures
Module 5: Advanced CL Commands
- Advanced File Operations
- Advanced Job Scheduling
- System Configuration Commands
- Security and Permissions