COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.

 View Only

Newer COBOL features, what should next?

  • 1.  Newer COBOL features, what should next?

    Posted 10 hours ago

    Open discussion.  
    I have been using user defined functions (and function prototypes) quite a bit recently.  They are nice, but there is currently one limitation that I consider to be pretty undesirable.  The data division "ANY LENGTH" clause, defined in the most recent COBOL standards, is not yet supported.  Here is what the standard says about this feature:  "The ANY LENGTH clause specifies that the length of a linkage section item may vary at runtime and is determined by the length of the argument in the calling program, method, or function."

    Basically, in the called routine (function or contained program) you can define a field in the linkage section with this ANY LENGTH clause, rather than having the length explicitly defined as part of the PICTURE clause.  This means, in theory, that a string of, well, any length could be passed as an argument ("actual parameter") and the called routine would treat the corresponding formal parameter as having the same length of that (passed) argument.

    As it stands currently, in the linkage section you must choose a specific size.  This size may be "too small" or "too large", since you really don't know what size strings users might want to call with.  Additionally, if you want to call it with a field that is smaller than the formal parameter defined in the function you either have to manually move it to a temporary field of the require size and pass that instead, or you have to pass it via an invocation of the CONTENT-OF intrinsic function (which will do this "under the covers").  And of course if you have a string larger than the size of the formal parameter, you have to write a new function that does exactly the same thing as the existing one, except allowing for larger input, or you can update the current one and recompile all modules that currently invoke it (possibly with changes if you did not use the CONTENT-OF method).  All in all "not ideal".

    I will say I understand why ANY LENGTH has not been chose to be supported up to this point.  The behavior of this feature would require some sort of implicit passing of not only the address of the passed field but also its actual length.  So it would likely either have to be passed implicitly as a separate "unseen" parameter, or maybe passed some other way (maybe a show parameter list pointed to by register 0 or something).  Currently COBOL, as far as I know, doesn't do anything like this, so I can see how its implementation might not be "straight forward".  That being said, it seems to me that it needs to be supported to make certain types of user defined functions truly useful.

    In fact, I would also like to see this feature allowed for "outer" programs and not used contained (nested) functions.  The COBOL standard says this is not supported:  "The ANY LENGTH clause cannot be specified in an outermost program. This is because an outermost program can be called with or without a program-prototype format CALL statement. For calls without a program-prototype, this International Standard does not require an implementation to determine whether an argument corresponds to a formal parameter described with ANY LENGTH."  While I understand the reasoning, I think it could possibly be worked around.  Firstly, program prototypes would have to be supported.  Then it seems to me there would have to be some way (perhaps passing another "implicit argument") for the activated program to know if it was invoked using a program prototype.  If it was invoked in any other manner (legacy COBOL call; call from a non-COBOL program; invoked directly by the operating environment (such as EXEC PGM=MYPROG in JCL)), if one of the formal parameters contains the ANY LENGTH clause, the program could abend.  Essentially, an outermost program containing an ANY LENGTH parameter would not be able to be invoked by any other method than from a COBOL program calling a "program prototype".

    Curious to hear what others think about this.  And are there any other features that IBM should give priority to implementing?  (I am just a user of Enterprise COBOL; I don't work for IBM or anything.)

    Here is a simple (if not particularly realistic) example of how a COBOL UDF might utilize this feature.

     identification division.
     function-id. string-validate-and-fix as 'STRVALFX'.
     
     environment division.
     configuration section.
     repository.
         function all intrinsic.
     
     data division.
     local-storage section.
     77  pos                         pic s9(9) comp.
     
     linkage section.
     01  string-in                   pic x any length.
     01  replacement-char            pic x.
     01  string-out                  pic x any length.
     
     procedure division using reference string-in
                              value replacement-char
                        returning string-out.
     routines section.
     mainline.
         move string-in to string-out
         if string-out is alphabetic
             continue
         else
             perform update-invalid-chars
         end-if
         goback.
     update-invalid-chars.
         perform varying pos from 1 by 1
                   until pos > length(string-out)
             if string-out(pos:1) is alphabetic
                 continue
             else
                 move replacement-char to string-out(pos:1)
             end-if
         end-perform
         exit.
     
     end function string-validate-and-fix.

    And here is how it could be used.

     move string-validate-and-fix(my-string ' ') to my-string

    The my-string field could be defined as PIC X, or PIC X(99999999), or anything in between (or even larger) and it should work.



    ------------------------------
    Frank Swarbrick
    ------------------------------