ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to dynamically generate a CL mbr add src in it, compile n execute it thru RPGLE

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to dynamically generate a CL mbr add src in it, compile n execute it thru RPGLE

    I have a requirement where in a RPGLE pgm will have to dynamically create a CL pgm / MBR , add in source in it may be through PF, compile it and execute it and send intimate the user.

  • #2
    The how would be pretty much as you describe. Your RPGLE program should write the source to a source file (writing it using embedded SQL would be easier, native IO does not really like writing actual source files), then it would run a compile command (either using QCMDEXC or a companion CL program). It could call it if you define a procedure prototype where the program name is a variable: "Dcl-pr MyNewPgm extpgm(variable_name) end-pr; and set vasriable_name to the name pf the program. Then when you do MyNewPgm(); it would call the new program assuming it was compiled into a library that a. is in the library list, and b. is in a library that is higher in the library list than any other libraries that contain the same program name.

    But not sure what you mean by "send intimate the user"

    And more important is the why - circumstances that require a custom program to be created on the fly are very rare, and most CL commands (basically any that does not make use of return variables) can be built as strings in an RPGLE program and then executed with QCMDEXC. So can you explain in more detail what this program will actually do, and maybe we can advise you better

    Comment


    • DipShell10
      DipShell10 commented
      Editing a comment
      Thanks for your reply!
      My requirement is like would design a RPGLE pgm which will be a source generator which will create a CL member, and generate code
      For generating code, it will have common code that is stored in the template file PF with priority field, so copy the records from that PF till the priority changes in the src member and then read the record from another file which is populated based on the input from the screen. So basically it can be copy file command or delete record or call program with the required parameters. We can have use CL as well if required.
      As of now I have to get this requirement clear....

      Send intimate the user means sending sending emails to the user that the code has been generated...

  • #3
    You misunderstand. I mean, your program is dynamically generating and running a CL program to do a task - what task? I suspect that task can be done in another way that does not require dynamically generating a CL program

    Comment


    • #4
      Ok let me make elaborate it. For each project that goes into the production we create a cl program source which do a cpyf and/or call a pgm to update certain file. This CL source has i/p parameters. So this source has to generate for every instance of the project. So user will enter necessary details in the screen and depending on that we are required to generate this CL source member and add in the required code in it. The common code will be has to be taken from a template file. After that compile the cl member as well.

      Comment


      • #5
        Is there anything that the CL has to do that can't just be done with QCMDEXC, calls to static CL programs and other RPG based functionality as Vectorspace suggests ?
        That way all the data entered for a project could be stored in a table and used to control a single program. Constantly generating and compiling sources, with the resultant need to have clean-up mechanisms for the sources and the PGM objects just seems a very clumsy way to do things.

        Comment


        • #6
          ... and what about copying and updating the files just with dynamic SQL (in RPG)?

          Comment


          • #7
            JonBoy I agree but the requirement is such that we have to generate the CL pgm and compile it dynamically. This CL source is required to take care of backing out the changes that are done in the project.
            I am just not sure how to go for it. Vectorspace can you pls explain your first post reply

            Comment


            • #8
              What others are suggesting, is that as your template files for instance contain CL instructions, why can't you call a program to execute those commands using QCMDEXC. Here's some pseudo code for a CL to give you an idea:

              Code:
              DCLF  <your template file>
              Loop until end of file
              RCVF
              CALL PGM(QCMDEXC) PARM(<source line in template file> <cmd length>)
              End loop
              This doesn't require creating and compiling temporary source files and is a lot cleaner.
              The same can be done in RPG by calling QCMDEXC from there.

              In your case, you'd build the complete set of commands in a file (e.g. in QTEMP or elsewhere if you need it kept) and a program simply reads the file and executes each instruction in it.

              Comment


              • #9
                Thanks!

                Also one of the requirement is how can we generate CL source on the fly... like we have some set of commands in the file how would we copy it into the CL source at one go and generate the code?


                Comment


                • #10
                  Thanks!

                  Also one of the requirement is how can we generate CL source on the fly... like we have some set of commands in the file how would we copy it into the CL source at one go and generate the code?


                  Comment


                  • #11
                    Thanks!
                    Also one of the requirement is how can we generate CL source on the fly... like we have some set of commands in the file how would we copy it into the CL source at one go and generate the code?

                    Comment


                    • #12
                      Generate your source file however you think.
                      Execute a CRTBNDCL command to compile the program from that source file into QTEMP, named, for example, DYNPGM1 (This can be done using QCMDEXC to execute the command, or your RPGLE can call a CL program that specifies the command)
                      Have your RPGLE program include this definitions:
                      Code:
                      dcl-pr dyamicProgram extpgm('QTEMP/DYNPGM1');
                      end-pr;
                      Then after you have compiled the dynamic CL program, your RPGLE program just has to do
                      Code:
                      call dyamicProgram();
                      And the program will be run

                      But I must reiterate. There should be no technical reason why you need to dynamically generate source and compile it into a one time use program. Any task you can do that way, you can do with static programs and/or QCMDEXC.
                      You simply say that it's the requirement. Does that mean someone has told you specifically to do it this way?
                      You say that it's to back out changes. So I assume the commands must be dynamic because they will be different for every project. But if you want to dynamically build and run a set of commands from data in a file, then the way to do that is to build each command as a string and execute each in turn with QCMDEXC. It will be far faster and far more reliable than dynamically generating a program.

                      Comment


                      • #13
                        What kind of file contains the commands you want to copy into a source file?

                        If it is a physical file, and if you want to copy all the records, you could use CPYF FMTOPT(*CVTSRC) to copy the file to a source physical file.

                        Comment


                        • #14
                          Could you take the template and add parameters to accept whatever is entered on the screen?

                          Or maybe use STRDBRDR

                          Originally posted by DipShell10 View Post
                          Ok let me make elaborate it. For each project that goes into the production we create a cl program source which do a cpyf and/or call a pgm to update certain file. This CL source has i/p parameters. So this source has to generate for every instance of the project. So user will enter necessary details in the screen and depending on that we are required to generate this CL source member and add in the required code in it. The common code will be has to be taken from a template file. After that compile the cl member as well.

                          Comment


                          • #15
                            Thanks Guys, with F specs keyword Extfile, extmember declare the srcfile , member(CL source ) and then add records in that file. CL source gets generated.


                            Comment

                            Working...
                            X