Saturday 12 October 2024

Creating Your First CL Program on IBM i (AS400)

 A CL program is a series of IBM i commands that automate tasks, manage system operations, and handle user interactions. CL programs can call other programs, submit jobs, process files, and display messages, among many other things. Unlike general-purpose programming languages like Java or Python, CL is primarily focused on interacting with the IBM i operating system to control system-level operations.


Steps to Create Your First CL Program

To build your first CL program, we will follow these steps:

  1. Create a Source File and Member: The file will store your CL program.
  2. Write the CL Program: Learn the basic structure and commands.
  3. Compile the CL Program: Convert the source code into a runnable object.
  4. Run the CL Program: Execute the compiled program to see it in action.

Step 1: Create a Source File and Member

Before you start writing your CL program, you need to create a source member within a source physical file. The source file is where your program's source code is stored, and each program resides as a separate member in that file.

Option 1: Using SEU (Source Entry Utility) and PDM (Program Development Manager)

  1. Log in to your IBM i system via a terminal emulator (e.g., IBM i Access Client Solutions or TN5250J).

  2. Create a source physical file if one does not exist already. For CL programs, the common source file name is QCLSRC:

    CRTSRCPF FILE(LIBRARY/QCLSRC) RCDLEN(112)
  3. Once the source file is created, start PDM to create a new source member:

    STRPDM
  4. Select Option 3 (Work with members) and specify the QCLSRC file and the library where it resides.

  5. Create a new member by typing 2=Create next to an empty line, and give the member a name (e.g., MYCLPGM), with the source type CLP.

Option 2: Using Visual Studio Code with the "Code for i" Plugin

  1. Open Visual Studio Code with the Code for i plugin installed.
  2. Connect to your IBM i system by configuring the host, username, and password.
  3. Navigate to the library and source file (e.g., QCLSRC) within the Remote Explorer.
  4. Right-click and create a new member for your CL program.

Option 3: Using RDi (Rational Developer for i)

  1. Launch Rational Developer for i (RDi).
  2. Navigate to the Remote Systems Explorer (RSE).
  3. Right-click on the QCLSRC source file and select New > Member.
  4. Name the source member and set the type to CLP.

Step 2: Writing the CL Program

Every CL program follows a basic structure that begins with PGM and ends with ENDPGM. The PGM command defines the start of the program, while ENDPGM signifies its end.

Basic CL Program Structure

PGM /* Your program logic goes here */ ENDPGM

Example: A Simple "Hello, World" Program

We will create a program that sends a message to the user. Since the DSPLY statement cannot be used in CL, we will use the correct command SNDPGMMSG (Send Program Message) to display a message.

Here is how your first program might look:

PGM SNDPGMMSG MSG('Hello, World!') ENDPGM

Let’s break this down:

  • PGM: Marks the start of the program.
  • SNDPGMMSG MSG('Hello, World!'): Sends the message "Hello, World!" to the user.
  • ENDPGM: Marks the end of the program.

Writing Your Program

  • If you are using SEU, navigate to the source member you created, and enter the CL program directly in the editor.
  • If you are using RDi or VS Code, open the newly created member and start typing your CL code there. These modern tools will assist you with syntax highlighting and error detection.

Step 3: Compiling the CL Program

Once you’ve written your program, the next step is to compile it. Compiling translates your source code into an executable object that can run on the IBM i system.

The CRTCLPGM command is used to compile a CL program. Here’s how to compile your program, depending on your tool:

Option 1: Using SEU/PDM

  1. Exit SEU after saving your program.

  2. On the IBM i command line, enter the following command:

    CRTCLPGM PGM(LIBRARY/PROGRAM_NAME) SRCFILE(LIBRARY/QCLSRC)

    Replace LIBRARY with the name of the library where your source resides and PROGRAM_NAME with the name of the source member you created.

  3. Press Enter to compile. If there are no syntax errors, the program will be compiled and available to run.

Option 2: Using Visual Studio Code with Code for i

  1. Right-click on the source member in VS Code.
  2. Select Compile from the options. The Code for i plugin will automatically run the CRTCLPGM command for you.

Option 3: Using RDi

  1. Right-click on the source member and select Compile.
  2. RDi will run the CRTCLPGM command and display any errors or warnings in the Problems view.

Step 4: Running the CL Program

Now that your program is compiled, it’s time to run it.

Option 1: Running from the Command Line

To run your program directly from the IBM i command line, use the CALL command:

CALL PGM(LIBRARY/PROGRAM_NAME)

This will execute the program, and you should see the message "Hello, World!" displayed on the screen.

Option 2: Running from a Modern IDE (RDi or VS Code)

  • RDi: You can submit the program to run directly from the IDE by right-clicking on the source member and selecting Run.
  • VS Code: Use the Remote Command feature in the Code for i plugin to run your CL program remotely.

Verifying the Output

If your program executes successfully, the system will send the message "Hello, World!" to the job's message queue. You can view this message by checking the Message Queue of your job. For example:

  • From the command line, type:

    DSPJOBLOG

    This will display the job log where you can see the message sent by your program.


Debugging and Troubleshooting

If your program fails to compile or does not behave as expected, here are a few common issues and tips to resolve them:

  1. Syntax Errors: If the program doesn’t compile, the system will provide a detailed error message. Double-check your code for typos or missing commands.
  2. Program Not Found: If you try to run the program and receive an error stating "Program not found," ensure that the program was compiled correctly and that the PGM parameter in the CALL command matches the correct library and program name.
  3. Checking Messages: If the program runs but the output doesn’t appear, check the message queue using the DSPJOBLOG command to see if the message was successfully sent.

Conclusion

Congratulations! You’ve created, compiled, and run your first CL program on IBM i. This simple "Hello, World!" program has introduced you to the basic structure and steps involved in CL programming.

In this article, you’ve learned how to:

  • Create a source file and member to store your program.
  • Write a basic CL program using the correct command (SNDPGMMSG).
  • Compile the program using the CRTCLPGM command.
  • Run the program using the CALL command and check for output.

In the next article, we’ll dive deeper into the topic of Data Types and Variables in CL Programming, where you’ll learn how to manage and manipulate data within your programs. Stay tuned!

No comments:

Post a Comment