📜 ⬆️ ⬇️

Meet COBOL - Part 1

With this article I plan to start a whole cycle, which may eventually be collected in a book. Information about COBOL in Russian is presented in fact by an article on Wikipedia and by two Soviet GOSTs. You can ask me why I'm starting to “dig up the corpse” and other similar questions. The answer is simple - just like that. Because I love COBOL, because it is not at all dead, because it is interesting, because it is part of an important story and it will continue to be part of the future. Because COBOL is an event. And let him be scolded by Dijkstra and others, it doesn’t diminish his volumes and his influence on IT in business.


Enough introduction, go to the most studied. COBOL is one of the oldest programming languages, its name as COmmon Business-Oriented Language, which appeared in 1959, is explained by the “grandmother” Grace Hopper, and is mainly used in various financial and administrative systems. For more information, you can find out its history in the nearest Internet search engine or (well, for absolutely lazy people) - notes.sochi.org.ru/1333 (Russian language) or en.wikipedia.org/wiki/COBOL (eng.).

In order to begin the practical study of the language, we need two things - a text editor (1 pc.) And a compiler (1 pc.)
If everything is simple with editors, even Notepad and vi, emacs, nano, etc. will do. etc. The main requirement “classic” is to be a text editor, not a text processor (I don’t remember what else was written in fairly old books on this topic ;-)).
')
Compilers are a little more complicated, there are commercial ones that cost a lot of money, there are student versions of commercial ones, there are versions of old compilers, there are even a few open source compilers. The choice is left to the reader, I will only provide a list with my comments.

www.opencobol.org OpenCOBOL (open-source, my personal recommendation, multiplatform)
tiny-cobol.sourceforge.net TinyCobol (open-source, according to the author, stopped development on January 1, 2011, and was not very active before that)
sourceforge.net/projects/cobolforgcc Cobol for GCC (open-source, pre-alpha, last updated 2009.)
homepages.paradise.net.nz/jsoeberg COBOL 12 (freeware, 16-bit compiler for DOS / Windows)
cev.cemotel.cz/cobol/cb_en/mx_all.htm Cevela MX COBOL (freeware, Windows)
www.microfocus.com/promotions/wwemvctd0510/default.aspx?page=form Micro Focus Visual COBOL R2 (trial, windows)
www.thekompany.com/products/kobol KOBOL (trial, linux, windows)
There is also a rich selection of ancient DOS compilers.

I personally use OpenCOBOL, which is to some extent the only real option at the moment.

Now that you have installed everything and are ready to rush into battle - let's begin. Naturally, with Hello World.

Type in the editor the following code:

 000100 IDENTIFICATION DIVISION.
 000200 PROGRAM-ID.  HELLOWORLD.
 000300 * --- This is an empty line.  ---
 000400 ENVIRONMENT DIVISION.
 000500 DATA DIVISION.
 000600 PROCEDURE DIVISION.
 000700 BEGIN.
 000800 DISPLAY “Hello World!”.
 000900 STOP RUN.


Safely save it to a file with a .cob (or .cbl) extension - these are the most popular, historical and logical extensions.
Next, in the terminal or command line, type cobc -x <file> and get the executable file. Run it and ooooop!

Hello World!

Now let's break this code down.

Let's start with the look. This is the “classic” code. With all the requirements that were 50 years ago. Why I use it:
1. Code in this format will suit any compiler.
2. This is “old school”, “Krutatenyushka” and I just like it.
3. It is always better to start with standard requirements, and then look at the capabilities of the compiler and your own taste.

The COBOL program string consists of 80 characters.
Character 1-6: line number (optional)
Symbol 7: “indicator”
* - comment line
- - the line “continuation”,
D line debug.
Symbol 8 - 11: Zone A. DIVISIONS, SECTIONS, names and headings of paragraphs, as well as indicators and numbers of “levels” should begin in it (all this will be discussed later).

Symbol 12-72: Zone B. Actually, the “code” expressions should begin by themselves.

Symbol 73-80: Comment Zone. It is not processed by the compiler and fully provided to the programmer.

With the horizontal plane figured out, let's go to the vertical.

Each COBOL program contains 4 sections - DIVISION, they go in a strict order and contain certain elements.

DIVISION FIRST. Inspiring.

IDENTIFICATION DIVISION. - describes the program and contains paragraphs such as

 PROGRAM-ID.  Helloworld.
 AUTHOR.  Beginner.
 INSTALLATION.  MyLocalCobolComputer.
 DATE-WRITTEN.  03/19/2011.
 DATE-COMPILED.  03/19/2011.
 SECURITY.  Iwillnottellanybodythiscode.


The content of these paragraphs is an ordinary commentary and, in principle, you can even write there “the year of the birth of Christ”.

DIVISION SECOND. Mystical.

ENVIRONMENT DIVISION. - Describes the environment, as the name implies, in which the program is written. Consists of two sections.

CONFIGURATION SECTION. Which includes paragraphs SOURCE-COMPUTER. and OBJECT-COMPUTER. and SPECIAL-NAMES. The first two bear a purely commenting function and describe on which computer, for which computer the program is written.
SPECIAL-NAMES. It is a rather deep thing, which will be considered later (or you can search for information on this topic for now).

INPUT-OUTPUT SECTION. Describes, as the name implies, input-output is very necessary, very important and very soon we will meet with it. Includes paragraphs FILE-CONTROL. and IO-CONTROL.

DIVISION THIRD. Legislative.

DATA DIVISION. - Contains descriptions of all variables. Includes 4 sections:
FILE SECTION. - describes the structure of files.
WORKING-STORAGE SECTION. - describes the variables.
LOCAL-STORAGE SECTION. - describes the variables that are created and initialized each time it is executed (in more detail in the following times).
LINKAGE SECTION. - describes the data that we receive when calling other programs.

DIVISION FOURTH. Working.

PROCEDURE DIVISION. - Contains the “very” program, which is divided into user sections, paragraphs, which contain expressions. In our case

BEGIN. - custom paragraph.

DISPLAY “Hello World!”.
STOP RUN. Actually expressions themselves.

And finally. Each expression must end with a period.

Thanks for attention. I am waiting for questions in the comments, which will necessarily be answered and included either in the following or in the editing of this article.



Vorontsov “nerfur” Vyacheslav. 2011

Source: https://habr.com/ru/post/115813/


All Articles