这个程序的流程是什么?
我遇到了一段让我困惑的 cobol 程序,this 是包含代码的页面,它试图演示 ALTER 有多么糟糕,但同时我不理解程序流程。
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.
在我看来,流程是这样的:
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.
2199-EXIT.
EXIT.
如果 ALTER 是要更改 GO-TO 的目的地,那么如果 GO-TO 已经执行并且程序退出了,那么它还有什么用呢?
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-PROCESS-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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://home.swbell.net/mck9/cobol/style/alter.html 对此进行了解释。您对于第一次执行是正确的,但是在后续运行中,行为发生了变化:
“我们第一次执行 PERFORM,控制权通过 2110-PROCESS-HEADER。但是,最后的 ALTER该段的内容更改了 2100-PROCESS-RECORD 中 GO TO 的目标,因此,在 PERFORM 的所有后续执行中,控制不会传递。 2110-PROCESS-HEADER。而是通过 2120-PROCESS-DETAIL。
“潜在的混乱是显而易见的。更改后的 GO TO 并没有到达它声称要去的地方——相反,它到达了某个远程代码段中指定的位置。要理解代码是如何工作的,你需要知道 ALTER 存在,并且需要知道执行 ALTER 的所有情况。”
这被称为自修改代码,并且很难理解和调试。对于一些简单的阅读睡前,我建议 http://www.pbm.com/~lindahl/mel.html< /a> 的Mel 的故事,
在 Cobol 中,EXIT 语句并不意味着“退出程序”:http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/evfeb4ls124.htm
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