REBOL for COBOL programmers

Go to table of contents Go to feedback page

nachalist start

Date written: October 25, 2012
Date revised:
Date reviewed:

This page gets going on the nachalist program, just to build up some momentum.


IDENTIFICATION DIVISION, so to speak

Start coding

Following earlier suggestions, we are going to copy our skeleton "IDENTIFICATION DIVISION" and fill it in, to get ourselves going. That would give something like the following result:

REBOL [
   Title:   "List a NACHA-format file"
   Date:    24-OCT-2012
   Name:    "nachalist"
   Version: "1.0"
   File:    %nachalist.r
   Author:  "Steven White
   Email:   cobolrebol@gmail.com
   Owner:   
   Purpose: {Lists a specified file that is in
   the "NACHA" format.
   }
   Comment: {

   }
]

Now, our procedure is going to be to follow the header with "do" statements that bring in all our pre-written modules. This doesn't have to be done here, but why not. It's a handy place to do it. All our programs are going to include our personal "global services module" whether they need it or not, just in case. Actually, it will turn out that almost all our program WILL need this module. So, after the header we code:

do %glb.r
If you put the global services module in some directory other than the one that holds the program, you can specify that full path name as the file name, including a drive letter if you like. For example,
do %/L/common-rebol-modules/glb.r

So, there we are, a lot of code created already. Now what. Well, this is going to be a simple program, that just asks for the name of a file and lists it, so we could ask for that file name. that would be done with the "request-file" function. You could enter it at the REBOL/View console prompt to try it out. Try

request-file/only
and see what happens. The "/only" refinement allows you to specify one file name only, and not several files. If you request one file only, the result returned will be one file name. If you leave off the "/only" refinement, the result returned will be a block of file names. If you cancel the request, the result returned will be "none."

Let us take this opportunity to discuss some details of how to code this operation. I COBOL, if you wanted to enter a file name, maybe to set the VALUE OF FILE-ID, you might code something like

WORKING-STORAGE SECTION.
77  WS-FILE-ID  PIC X(100) VALUE SPACES.
... some more code ...
PROCEDURE-DIVISION.
MAIN-PARAGRAPH.
    ACCEPT WS-FILE-ID.
You could mimic this in REBOL, something like
;; Working storage
WS-FILE-ID: none
... some more code ...
;; Procedure division
WS-FILE-ID: request-file/only 
But just because something is done one way on one language, does not mean it is the best way. In REBOL you could, for one thing, put those two statements right next to each other. That is, you don't have to "define" data items in one place and then refer to them in code in some other place. But, you don't even have to "define" WS-FILE-ID at all. You could just code
WS-FILE-ID: request-file/only
without that other line that "defines" WS-FILE-ID by setting its initial value to "none." But even that is more than is necessary for REBOL. If you were just going to read a file one time, and did not want to keep track of the file name (which you might do for debugging purposes), you could code
FILE-DATA: read/lines request-file/only
You could push it even more if you just wanted to, for example, read the first line of the file.
print first read/lines request-file/only

Being a COBOL programmer, I am "programmed" to take a more plodding approach. In this sample program, we are going to request the file name as a separate step, and then read the file based on that name This will make the program wordier than is acceptable to REBOL experts. However, it will help organize the program, and it will keep that file name around in case we want to display it somewhere for debugging. So the final code for accepting a the name of the input file will look something like this:

NACHA-FILE-ID: request-file/only
if not file? NACHA-FILE-ID [
    alert "No file was selected"
    quit
]
The extra code after the request-file is present to take care of the possibility that a person will not select a file. If a file is selected, NACHA-FILE-ID will be a file name. If anything else is done with the request window, NACHA-FILE-ID will not be a file name. So we can check for that one thing, and just quit if no file is selected.

Before we code more of the program, we have to explore some more of that idea of the personal mezzanine, as explained in the following topic.