• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Learn %PARMS! Solve Two CL Problems!

    October 8, 2018 Ted Holt

    Those wonderful people at IBM have done it yet again! They have gladdened my existence with new CL functionality that solves two problems, and I will never have to face those problems again. Let me tell you about the new %PARMS built-in function.

    The %PARMS function returns the number of parameters that are passed into a CL procedure (i.e. a CL program or a CL module). In the past, I have monitored for message MCH3601. That works in some situations, but not in all. The %PARMS function gives me an unambiguous way to know whether a parameter was passed or not. IBM released it for IBM i 7.3 in PTF SI66721.

    I’ll gladly illustrate the two problems and show how to use %PARMS to avoid them.

    The Program Problem

    Consider program PARMTEST:

    pgm (&p1 &p2 &p3)
    
       dcl &p1     *char    3
       dcl &p2     *char    3
       dcl &p3     *char    3
    
       dcl &msg    *char   14
    
       ChgVar  &msg  ('/' *cat &p1 *cat ' ' *cat +
                               &p2 *cat ' ' *cat +
                               &p3 *cat '/')
    
       SndPgmMsg  msg(&msg) MsgType(*comp)
    

    If I call this program and pass it three parameters, I see a message that contains the values I passed in.

    call PARMTEST (AAA BBB CCC)
    /AAA BBB CCC/
    

    But if I call it with fewer than three parameters, I see something else.

    call PARMTEST (AAA BBB)
    MCH3601	Pointer not set for location referenced.
    CPF9999	Function check. MCH3601 unmonitored by PARMTEST at statement 0000000900, instruction X'0000'.
    CPA0702	MCH3601 received by procedure PARMTEST. (C D I R)
    

    The CHGVAR command canceled because I did not supply a parameter value. Here’s how %PARMS allows me to avoid that problem.

    pgm (&p1 &p2 &p3)
    
       dcl &p1     *char    3
       dcl &p2     *char    3
       dcl &p3     *char    3
    
       dcl &x1     *char    3   value('DFT')
       dcl &x2     *char    3   value('DFT')
       dcl &x3     *char    3   value('DFT')
    
       dcl &msg    *char   14
    
       if (%parms *ge 1) then(ChgVar &x1 &p1)
       if (%parms *ge 2) then(ChgVar &x2 &p2)
       if (%parms *ge 3) then(ChgVar &x3 &p3)
    
       ChgVar  &msg  ('/' *cat &x1 *cat ' ' *cat +
                               &x2 *cat ' ' *cat +
                               &x3 *cat '/')
       SndPgmMsg  msg(&msg) MsgType(*comp)
    

    I rewrote the code so that the CHGVAR command no longer refers to the parameter variables, but to local variables instead. I load the local variables from the parameters, but only if those parameters were passed to the program. Otherwise, the local variables have default values.

    call PARMTEST (AAA BBB CCC)
    /AAA BBB CCC/
    call PARMTEST (AAA BBB)
    /AAA BBB DFT/
    call PARMTEST (AAA)
    /AAA DFT DFT/
    call PARMTEST
    /DFT DFT DFT/
    

    Problem number one is solved. Now let me show you problem number two.

    The Module Problem

    Let’s consider the same code, except that PARMTEST is compiled as a module, not a program. Here’s the source code for module CALLER, which calls module PARMTEST.

    pgm
    
       CallPrc  PARMTEST  (AAA BBB CCC)
       CallPrc  PARMTEST  (DDD EEE)
       CallPrc  PARMTEST  (FFF)
       CallPrc  PARMTEST
    
    endpgm
    

    I use the Create CL Module (CRTCLMOD) command (option 15 in PDM) to create both modules and CRTPGM to bind them into one program.

    CRTCLMOD MODULE(MYLIB/PARMTEST) +
       SRCFILE(QCLSRC) SRCMBR(PARMTEST)
    CRTCLMOD MODULE(MYLIB/CALLER) +
       SRCFILE(QCLSRC) SRCMBR(CALLER)
    CRTPGM PGM(CALLER) MODULE(CALLER PARMTEST) +
       ACTGRP(*NEW)
    

    Now I have a program. Here’s what happens when I call it.

    CALL CALLER
    /AAA BBB CCC/
    /DDD EEE CCC/
    /FFF EEE CCC/
    /FFF EEE CCC/
    

    CALLER thinks I passed all three parameters, even when I didn’t. When I don’t supply a parameter, CALLER uses the data from an earlier call, as the pointers to that data just happen to still be in memory. This can certainly lead to erroneous, unpredictable, undesirable results.

    However, if I use the new version of the PARMTEST module instead, I see this:

    CALL CALLER
    /AAA BBB CCC/ 
    /DDD EEE DFT/ 
    /FFF DFT DFT/ 
    /DFT DFT DFT/
    

    Thanks to %PARMS, I was able to use default values for unpassed parameters.

    Will I ever monitor for MCH3601 again? Who knows? It doesn’t look very likely that I will.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: %PARMS, 400guru, CL, FHG, Four Hundred Guru, IBM i

    Sponsored by
    Raz-Lee Security

    With COVID-19 wreaking havoc, cybercriminals are taking advantage of the global impact that it has had on our families, our businesses and our societies. It is more important now than ever to ensure that IT systems are protected, so that when all of this is behind us, we can get back to business as usual as quickly as possible.

    iSecurity Anti-Ransomware protects organizations against ransomware attacks and other kinds of malware that may access and change business-critical data on your IBM i. It even protects against zero-day attacks. Anti-Viruses can only report on the damage an attack has caused, but not stop it.

    iSecurity Anti-Ransomware has been recently enhanced with a Self-Test feature that allows you to simulate a ransomware attack on your IBM i. The simulated attack is limited to the test folder and cannot harm any other folders or files. This new feature lets organizations see how they are protected against known or unknown ransomware.

    Key Features:

    • Real-time scanning for known and unknown ransomware threats.
    • Blocks and disconnects the intruder.
    • Instantaneously sends alerts to SIEM as well as the offending computer.
    • Self-Test for attack simulation
    • Classification of the attack based on log.
    • Automatic updates with the most current ransomware definitions.

    Contact us at https://www.razlee.com/anti-ransomware

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: How Can You Think That? Modern Lessons From A Fresh IBM i Developer

    2 thoughts on “Guru: Learn %PARMS! Solve Two CL Problems!”

    • Michael says:
      October 8, 2018 at 10:48 am

      Typo, MCH3601 not MCH3061 🙂

      Reply
    • Doug S says:
      November 2, 2018 at 1:01 pm

      Is there a version for 7.2?

      Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 67

This Issue Sponsored By

  • UCG Technologies
  • Rocket Software
  • SEA
  • Computer Keyes
  • WorksRight Software

Table of Contents

  • Riding The Upgrade Cycle
  • Modern Lessons From A Fresh IBM i Developer
  • Guru: Learn %PARMS! Solve Two CL Problems!
  • As I See It: How Can You Think That?
  • The Cloud Changes Things, But Not Everything

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Power10 Midrange Machine: The Power E1050
  • IBM Puts The Finishing Touches On PowerHA For IBM i 7.5
  • Guru: Regular Expressions, Part 2
  • Get Your Security Education, And Not From The School Of Hard Knocks
  • IBM i PTF Guide, Volume 24, Number 33
  • Power10 Entry Machines: The Power S1024 And Power L1024
  • Thoroughly Modern: Latest IT Trends – Bring Security, Speed, And Consistency To IT With Automation
  • Big Blue Unveils New Scalable VTL For IBM i
  • As I See It: Thank God It’s Thursday
  • IBM i PTF Guide, Volume 24, Number 32

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.