Once in a while I decide I want to play with COBOL, so I install GNU COBOL, dig out my old test code and try to use it to do something interesting.
So, most recently I wanted to simply read a file, do some simple processing and then write an output file. Very basic, right?
Not so much with COBOL.
First you have to statically declare the files you want to use. Let's say you want to read from "foo.txt":
environment division.
input-output section.
file-control.
select foo0 assign to "foo.txt"
organization is sequential
access mode is sequential
file status is fs0.
You then need to declare (statically again, of course) the structure of the records in the file. You can think of this as global variables that gets filled in when reading from the file:
data division.
file section.
fd foo0.
01 foo0-rec.
02 foo-name pic x(40).
02 foo-value pic x(20).
You also need to declare the "fs0" variable:
working-storage section.
01 fs0 pic 99.
Then, you need to open the file. That's not too hard:
open input foo0.
Then you want to read from it. You can do that using the command:
read foo0.
After the read, the variable "fs0" will be 0 when you've reached the end of the file, so you need a loop that, I presume (I never got this part to work right), would look something like:
perform with test before until fs0 = 0
read foo0
* foo-name and foo-value now contains data from the file
end-perform
After all of this, I decided I wanted to do something different. I wanted to call a web service to load some information over HTTP. I dropped that project really quickly once I realised that the only way to do that was to use libcurl and directly access it using FFI. The FFI that is provided by GNU COBOL isn't really easy to work with, as evidenced by this discussion:
https://stackoverflow.com/questions/26367026/how-to-make-htt...If you look at the code that is linked from that page, you'll rather quickly realise why COBOL is not such a great idea for general purpose computing.