RPGLE 中的原型和界面 programs/procedures

Prototype and interface in RPGLE programs/procedures

阅读 IBM 文档后,我对如何正确编写 program/procedure 入口点感到有点困惑。

尤其是读这个https://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c092508410.htm
我看到了:

The prototype with the matching name must precede the procedure interface in the source

这里http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzasd/freeinterface.htm
我读:

If you do not specify a prototype for a cycle-main procedure, you use *N as the name for the procedure interface

所以我用这种方式编写了一个新程序:

CTL-OPT ...

DCL-PI *N;
  P1 CHAR(2);
  P2 CHAR(2) CONST;
  P3 CHAR(2) CONST;
  P4 CHAR(1) CONST;
  P5 PACKED(7) CONST;
  P6 POINTER;
  P7 INT(5);
END-PI;

...

它编译并且运行很好。
那么我想问的是,什么时候以及为什么要在界面之前指定一个原型?

所以,详细了解并查看 7.1 中的更改 >(对于完整的自由格式语法)我发现了这个:
What's New in 7.1?

If a program or procedure is not called by another RPG module, it is optional to specify the prototype. The prototype may be omitted for the following types of programs and procedures: A program that is only intended to be used as an exit program or as the command-processing program for a command A program that is only intended to be called from a different programming language A procedure that is not exported from the module A procedure that is exported from the module but only intended to be called from a different programming language.

所有 "What's new" 页都值得好好阅读!

对您问题的简短回答始终是为 RPG4 程序创建原型。它可能是可选的,但您以后可能会需要它。

我已经开始对程序使用线性主程序而不是循环主程序。您可以通过在程序开头包含 ctl-opt Main(procname) 来实现。我还将我的原型放在单独的源文件中。这样我就可以轻松地将它们包含在 /copy 指令中,这有一个副作用,即在我的程序顶部附近收集外部程序和服务程序依赖项。我更喜欢将所有原型放在一个单独的源文件中,与程序源文件同名。或者对于服务程序,我把它命名为与服务程序一样。但是我工作过的一些地方会在原型的最后加上一个后缀,和程序或者模块源一起存放在同一个服务程序中。 v7.1 的好处是您不必为内部过程定义原型,只需定义导出的原型即可。在程序级别,这是主要过程,虽然这不是严格要求的,但我总是在它自己的源代码中创建该原型,因为你永远不知道你是否想在某个时间从其他地方调用该程序在将来。此外,虽然您不需要在程序本身中包含原型,但这样做是验证原型是否有效的好方法。

关于必须手动打开和关闭文件...

RPG 模块中定义的所有文件都会自动打开,除非指定了 USROPN 关键字。这适用于任何类型模块中的全局文件,以及子过程中的本地文件。

不同之处在于文件何时或是否自动关闭,具体取决于声明文件的位置。在激活组结束之前,子过程中的静态文件或 NOMAIN 或线性主模块中的全局文件永远不会自动关闭。子程序结束自动关闭子程序文件,循环主模块全局文件根据RPG循环关闭。