开发者

what is the flow in this program?

I came across a piece of cobol program which got me confused, this is the page containing the code, it try to demonstrate how bad ALTER is but at the same I don't understand the program flow.

     PERFORM 2100-PROCESS-RECORD THRU 2199-EXIT.
...
 2100-PROCESS-RECORD. 
     GO TO 2110-PROCESS-HEADER.
*
 2110-PROCESS-HEADER.
* code to process a file header
     ALTER 2100-PROCESS-RECORD TO 2120-PROCESS-DETAIL.
     GO TO 2199-EXIT.
*
 2120-PROCESS-DETAIL.
* code to process a detail record 
     GO TO 2199-EXIT.
...
*
 2199-EXIT.
     EXIT.

In my mind, the flow is like this:

     PERFORM 2100-PROCESS-RECORD THRU 2199-EXIT.
...
 2100-PROCESS-RECORD. 
     GO TO 2110-PROCESS-HEADER.
*
 2110-PROC开发者_C百科ESS-HEADER.
* code to process a file header
     ALTER 2100-PROCESS-RECORD TO 2120-PROCESS-DETAIL.
     GO TO 2199-EXIT.

 2199-EXIT.
     EXIT.

If ALTER is to change the destination of a GO-TO, how can it be useful if the GO-TO was already executed and the program exited?


http://home.swbell.net/mck9/cobol/style/alter.html explains it. you're correct for the first execution, but on subsequent runs the behavior is changed:

"The first time we execute the PERFORM, control passes through 2110-PROCESS-HEADER. However, the ALTER at the end of that paragraph changes the destination of the GO TO in 2100-PROCESS-RECORD. As a result, on all subsequent executions of the PERFORM, control does not pass through 2110-PROCESS-HEADER. It passes through 2120-PROCESS-DETAIL instead.

"The potential for confusion is obvious. The altered GO TO does not go where it claims to go -- instead, it goes to a place specified in some remote piece of code. To understand how the code works you need to know that the ALTER is present, and you need to know all the circumstances which execute the ALTER."

it's called self-modifying code, and it's very hard to understand and debug. for some light reading before bed, I recommend http://www.pbm.com/~lindahl/mel.html the story of Mel.

in Cobol, the EXIT statement does not mean "exit the program": http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/evfeb4ls124.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜