v6r1 rpg enhancements

Upload: rachmat99

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 V6R1 RPG Enhancements

    1/21

  • 7/28/2019 V6R1 RPG Enhancements

    2/21

    This is an enhancement that is long overdue. More and more of our programs should be making use of memory, but wehave been restricted by the 64K limit in RPG. This enhancement means that a whole lot of dynamic memory

    management procedures and pointer/user space procedures are about to disappear. It also means that operations suchas XML-INTO are now more functional and less cumbersome to use.

    (1) D LongFld S a Len(25000000)

    (2) D BigDS DS a Len(60000000)

    (3) D BigArray1 S a Len(10000000)

    D Dim(6)

    (4) D BigArray2 S 1a Dim(50000000)

    (5) D BigVarying S a Len(25000000) Varying(4)

    D DummyPtr S *

    /Free

    (6) DummyPtr = %Addr(BigVarying : *Data);

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    3/21

    Figure 1: Use the LEN keyword to define a large length.

    Figure 1 highlights some of the changes in relation to the definition of large variables (refer to the numbers above).

    1. Since only seven positions are available to specify the length of a field in the D spec, you use the LEN keyword tospecify a length greater than 9,999,999.

    2. The LEN keyword may also be applied to a data structure.

    3. The LEN keyword may also be applied to an array.

    4. Larger variable sizes also mean you can have a larger number of elements in an array as long as the total storage forthe array does not exceed 16M. The same applies to multiple-occurrence data structures.

    5. Varying-length fields now require 2 or 4 bytes to store the actual length of the field. A size of 2 is assumed if the

    specified length is between 1 and 65535; otherwise, a size of 4 is assumed. You can specify either VARYING(2) orVARYING(4) for definitions whose length is between 1 and 65535. For definitions whose length is greater than 65535,VARYING(4) is required.

    6. The %ADDR BIF has also been enhanced. The optional *DATA parameter may be specified so that %ADDR returnsthe address of the data portion of a variable-length field.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    4/21

    UCS-2 and Graphic fields can have a maximum length of 8M.

    The maximum size of literals has also increased:

    Character literals can now have a length of up to 16380 characters.

    UCS-2 literals can now have a length of up to 8190 UCS-2 characters.

    Graphic literals can now have a length of up to 16379 DBCS characters.

    For those of you who program with different character sets in mind, some of the UCS-2 rules have been relaxed. Tomake it easier to change the data type of database fields to be UCS-2, the compiler has changed to allow any of thestring types to be used in assignment and comparison operations without explicit conversion. The compiler performs anyneeded conversions implicitly. UCS-2 variables can now be initialized with character or graphic literals without using the%UCS2 built-in function. UCS-2 enhancements are available as PTFs back to V5R3.

    Not that I'm greedy or anything but when can we have terabyte fields? Files Defined in Subprocedures

    Subprocedures have F-specs. Read that again: subprocedures have F-specs. This means that a file defined in asubprocedure is local to the subprocedure.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    5/21

    P GetName B

    FCustomer IP E K Disk

    D GetName PI 50a

    D CusNo 10a

    D GetCustRec Ds LikeRec(CustomerR)

    /Free

    Chain Cusno Customer GetCustRec;

    Return GetCustRec.CustName;

    /End-free

    P E

    Figure 2: Here's an example of a file defined in a subprocedure.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    6/21

    Figure 2 shows an example of a subprocedure that returns a Customer name. A few points are worth noting:

    Input and output specifications are not generated for local files; therefore, all input and output must be done withresult data structures (hence the definition of the GetCustRec data structure in Figure 2).

    By default, files are automatically opened when the subprocedure is called and automatically closed when thesubprocedure ends (either normally or abnormally). Of course, USROPN may be specified for a file so the opening andclosing of the file are under your control in the subprocedure.

    You can change the default opening and closing of the file by specifying the STATIC keyword on the F-spec. Thismeans that the storage associated with the file is static and all invocations of the subprocedure will use the same file. Ifthe file is open when the procedure returns, it will remain open for the next call to the procedure. Templates

    The TEMPLATE keyword allows you to define template data structures, standalone fields, and files.

    Templates for Data Structures

    The concept of a template for data structures is not new. Figure 3 shows the traditional way of defining and using avirtual template. The data structure Phone contains the definition of a phone number (complicated things, really); thedata structure is based on a pointer (DummyPtr), which is never set, so it does not occupy any storage in the program.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    7/21

    The data structures to be used in the program, HomePhone, CellPhone and WorkPhone, are all defined usingLIKEDS(PHONE). Any required subfield initialization is performed in the program. The main problem with this method isthat you have to initialize the subfields in the calculation code of the program.

    D Phone DS Based(DummyPtr) Qualified

    D CountryCode 5i 0

    D NDDPrefix 5

    D AreaCode 5

    D Number 9

    D Extension 4

    D IDDPrefix 5

    D HomePhone DS LikeDS(Phone)

    D CellPhone DS LikeDS(Phone)

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    8/21

    D WorkPhone DS LikeDS(Phone)

    /Free

    HomePhone.CountryCode = 353;

    CellPhone.CountryCode = 353;

    WorkPhone.CountryCode = 353;

    Figure 3: This virtual template uses BASED and LIKEDS.

    Figure 4 show the comparable structures (to Figure 3) being defined with the new TEMPLATE keyword. At first glance,they are fairly similar, the major difference being the use of the INT keyword on the DS definition of PHONE and theINZ(353) for the CountryCode subfield in Phone. The use of the INZ keyword with the template data structure means thesame initialization may be applied to any dependent data structures (defined using LIKEDS) by specifying INZ(*LIKEDS).

    D Phone DS Template Inz

    D CountryCode 5i 0 Inz(353)

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    9/21

    D NDDPrefix 5

    D AreaCode 5

    D Number 9

    D Extension 4

    D IDDPrefix 5

    D HomePhone DS LikeDS(Phone)

    D Inz(*LikeDS)

    D CellPhone DS LikeDS(Phone)

    D Inz(*LikeDS)

    D WorkPhone DS LikeDS(Phone)

    D Inz(*LikeDS)

    Figure 4: Use the TEMPLATE keyword to define a data structure.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    10/21

    The TEMPLATE keyword may also be applied to a standalone field.

    A definition defined with a TEMPLATE keyword may only be used as a parameter for the LIKE or LIKEDS keywords orthe %SIZE, %LEN, %ELEM, or %DECPOS BIFs; it may not be used as a normal data structure or field.

    Templates for Files

    The TEMPLATE keyword may be specified for files. Files defined with the TEMPLATE keyword are not included in theprogram; the file definition is used only at compile time. The template file can only be used as a basis for defining otherfiles later in the program using the new LIKEFILE keyword. The LIKEFILE keyword also allows you to pass a file as aparameter.

    Let's look at an example:

    (1) FCustomer IF E K Disk Template

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    11/21

    P GetCustomer B

    D GetCustomer PI N

    (2) D customerFile LikeFile(Customer)

    (3) D customerData LikeRec(CustomerR)

    D customerKey Like(customerData.CustNo)

    D Const

    /Free

    (4) Chain customerKey customerFile customerData;

    If %Found(customerFile);

    If customerData.Status 'D';

    Return *On;

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    12/21

    EndIf;

    EndIf;

    Return *Off;

    /End-Free

    P E

    Figure 5: Here's a subprocedure to get a customer record.

    Figure 5 shows a portion of a member containing a subprocedure (GetCustomer) that retrieves customer data. Thismember is compiled and placed in a service program. The main points to note are these (refer to the numbers above):

    1. The TEMPLATE keyword is specified for the customer file. This means that the File specification is for referencepurposes only, and the file definition may not be used for processing.

    2. The LIKEFILE keyword identifies the first parameter as a file with the same characteristics as the Customer file.

    3. The second parameter is the customer record that will be returned by the subprocedure.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    13/21

    4. The file parameter name is used to identify the file to be processed (i.e., customerFile not Customer). Since Input andOutput specs are not generated for a template file or a file identified by LIKEFILE, you must use a result data structurefor the CHAIN operation.

    How do you call the GetCustomer subprocedure? Figure 6 shows a snippet of a program that performs a call. The file tobe processed (CustFile) is simply passed as the first parameter. The format of the file CustFile must be the same as theCustomer file.

    FCustFile IF E K Disk

    D custData DS LikeRec(CustFileR)

    /Free

    If GetCustomer(CustFile: CustFileR: 'THISCUST');

    // DO cool things with data

    EndIf;

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    14/21

    Figure 6: Call the GetCustomer subprocedure.

    The LIKEFILE keyword may also be used on the F-specs. Figure 7 shows an example of two files (CurrCust andOldCust) being defined like the Customer file. The processing options (file type, record addition, record address type,device, etc.) and most (but not all) of the keywords are inherited.

    FCustomer IF E K Disk Template Block(*YES)

    FCurrCust LikeFile(Customer)

    F ExtFile('CURRLIB/CUSTFILE')

    FOldCust LikeFile(Customer)

    F ExtFile('OLDLIB/CUSTFILE')

    Figure 7: Using LIKEFILE on the File Specifications.

    There are a few items to bear in mind when using LIKEFILE:

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    15/21

    The parent file must be externally defined.

    Files are implicitly qualified; therefore, resulting data structures are required for input and output operations.

    The parent file must define any blocking requirements.

    Not all keywords are inherited. These are keywords that must be unique for each file (e.g., INDDS, INFDS,INFSR, OFLIND).

    Although the SFILE keyword may be inherited, you still need to define it for dependent files in order to specify aunique RRN field. Other File Enhancements

    There are a few other file related enhancements worth having a look at.

    (1) FCustomer IP E K Disk ExtDesc('MYLIB/CUSTFILE')

    (2) F ExtFile(*ExtDesc)

    (3) F Qualified

    FScreens CF E WorkStn

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    16/21

    D GetCustRec Ds LikeRec(CustomerR)

    (4) D GetDetails E Ds ExtName('MYLIB/SCREENS' :

    D Screen1 : *ALL)

    /Free

    (5) Read Customer.CustomerR GetCustRec;

    If GetCustRec.Type = 1;

    Eval-Corr GetDetails = GetCustRec;

    (6) ExFmt Screen1 GetDetails;

    EndIf;

    Figure 8: Note these other file enhancements.

    Figure 8 shows some of the other file enhancements introduced in V6R1 (refer to the numbers above).

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    17/21

    1. You are aware that the EXTFILE keyword allows you to specify the file to be used when a program is called (a built-inoverride), but EXTFILE does not have any effect at compile time. The EXTDESC keyword allows you to specify the filedefinition to be referenced at compile time. This provides a means of handling SQL defined tables (where the file and

    format name are the same) other than renaming the record format.

    2. The EXTFILE keyword allows a special value of *EXTDESC, which means that the value specified for the EXTDESCkeyword should be used by the EXTFILE. Basically, you are specifying the same value for both the EXTDESC (compiletime) and EXTFILE (run time) keywords.

    3. The QUALIFIED keyword may be specified for files. This means that all references (except for the RENAME,

    INCLUDE, IGNORE, and SFILE file keywords) to record format names must be qualified with the file name.

    4. The file name specified on the EXTNAME keyword may be a character literal in any of the forms 'LIBRARY/FILE','FILE', or '*LIBL/FILE'.

    5. As with file specifications in subprocedures, Input and Output specifications are not generated for a qualified file (i.e.,external fields from the file are not automatically defined as fields in the program and all I/O to the file must be done with

    result data structures).

    6. A data structure name may be specified as the result for an EXFMT operation. This eases the use of qualified datastructures with display files. The *ALL value must be specified on the LIKREC or EXTNAME keyword for the datastructure. No Cycle RPG

    If you have delved into the wonderful world of ILE, you are almost certain to have coded a module with the NOMAINkeyword in the control specifications. This means that the module only contains global definitions (F- and D-specs) andsubprocedures and that the compiler does not place any RPG cycle code in the module since there is no mainline (alinear module). Since a NOMAIN module does not contain a Program Entry Procedure (PEP), it cannot be compiled as acallable program.

    The introduction of the MAIN keyword on the control specification allows you to code a module that may be created as aprogram but does not contain the RPG cycle. The MAIN keyword allows you to specify the name of the subprocedure tobe used as the PEP for the program. Figure 9 shows the code in a member named CUST001. The member is compiledusing the CRTBNDRPG command. The MAIN keyword identifies the MaintainCustomer subprocedure as the PEP for the

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    18/21

    program.

    H Main(MaintainCustomer)

    D/Copy Prototypes

    P MaintainCustomer...

    P B

    D MaintainCustomer...

    D PI

    /Free

    // Lots of cool code

    /End-Free

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    19/21

    P E

    Figure 9: This example shows how to use the MAIN keyword.

    The other major difference is with the prototype for the subprocedure. Figure 10 shows the prototype (defined in theProtoTypes member) for the MaintainCustomer subprocedure defined in Figure 9. Although the prototype is for asubprocedure, the EXTPGM keyword is used to identify the program in which the subprocedure is defined.

    D MaintainCustomer...

    D PR ExtPgm('CUST001')

    Figure 10: Here's the prototype for a subprocedure identified on the MAIN keyword.

    The ability to identify the PEP for a program also means that a program may be called recursively. This avails of thealready available feature whereby subprocedures can be called recursively.

    Threads

    The threads enhancement is probably of little immediate benefit to most of us but will become increasingly important asRPG programs and subprocedures are remodeled to be used as Web services.

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    20/21

    RPG can run safely in multiple threads, but each RPG module can be accessed by only one thread at a time. This meansthat in a services environment, RPG modules could be a potential bottleneck impacting performance and scalability.V6R1 introduces the option of having RPG modules run concurrently in multiple threads.

    Specifying THREAD(*CONCURRENT) on the Control specification has these results:

    Multiple threads can run in the module at the same time.

    By default, static variables are defined so that each thread has its own copy of the static variable.

    Individual variables can be shared by all threads using STATIC(*ALLTHREAD).

    Specifying SERIALIZE on the Procedure- Begin specification means individual procedures can be serialized sothat only one thread can run them at one time.

    The total amount of static storage is greater because each thread has its own copy of the static storage. Compile Options

    Lastly, here are a couple of options you can take when you are creating modules or programs.

    The OPTION keyword on the CRTBNDRPG and CRTRPGMOD commands (or in the control specification) may

    MC Press Online

    http://www.mcpressonline.com Powered by Joomla! Generated: 29 August, 2008, 00:08

  • 7/28/2019 V6R1 RPG Enhancements

    21/21

    specify a value of *UNREF (the default) or *NOUNREF. *NOUNREF indicates that unreferenced variables should not begenerated into the RPG module. This can reduce program size and can reduce the time taken to bind a module to aprogram or service program. This may be particularly useful in a multi-threading environment where static variables aredefined for each thread.

    The PGMINFO keyword on the CRTBNDRPG and CRTRPGMOD commands (or in the control specification) mayspecify that PCM information is to be stored in a file in the IFS (identified by the INFOSTMF parameter) and/or themodule itself. The PCML information becomes part of the program or service program and may be retrieved using theQBNRPII API. New Ways of Doing Things

    Just when I am getting comfortable with the way I am programming, along comes IBM with new and improved ways ofdoing things. As with all enhancements, some of these will be of immediate use (larger variable sizes, files insubprocedures, templates), some will be of occasional use (passing files as parameters), and others are for the future(concurrent threads).

    RPG is certainly doing a lot of kicking and screaming for a language that some say is dying. It won't go quietly.

    MC Press Online