Sunday, January 20, 2019

Installing COBOL on Mac and running "hello Cobol!" program

It may sound a bit crazy, due to my main focus but yes, the title is correct. It does contain word COBOL. Actually the acronym is pretty nice, it means "Common Business-Oriented Language" = COBOL. According to resources COBOL was the 1st popular language designed to be operating system agnostic. It is imperative, procedural language. Oh, It's fair to mention that since 2002 it's also Object-Oriented but I may touch this in one of future post. This short post is more focused on how to run COBOL on a Mac. 

Why COBOL ? True is that my 1st touch with COBOL was at SUN Microsystem in around 2004 or 2005. I don't exactly remember but it was due to some Bank system project. Yes, Cobol is even nowadays widely used in Government, Banks or Industrial systems. Maybe not so much on mainframes but ;) they are still there too. COBOL is used in many legacy applications such as batch and transaction processing jobs. Actually you may be surprised how JVM helps to run COBOL programs, but this is out of the scope of this short post.

1. Installing a COBOL compiler on MAC Mojave. 
I'll use the homebrew (The missing package manager for the macOS). Open your terminal and type:


$ brew install gnu-cobol  

2. Writing the 1st COBOL Program
I'm using VIM on Mac. Sure when you are on Windows or Linux machine you have a possibility to use Visual COBOL for Eclipse from MicroFocus which has a lot more features, or maybe others but I'm familiar with this one. For this post purposes the VIM is more than enough. 
Open the terminal create a "hello_cobol.cob" file with the following content:
$ vi hello_cobol.cob

--- hello_cobol.cob ---
program-id. hello_cobol.

data division.
working-storage section.
01 HELLO_MESSAGE PIC X(12) VALUE "Hello COBOL!".

procedure division.
        display HELLO_MESSAGE.
        goback.


end program hello_cobol.
---

3. Compiling and running the program
As you can see code is very nicely separated into the sections. To get more information about the code structure see for example following guide. To compile a code executed the following command:


$ cobc -free -x hello_cobol.cob

After the successful execution the hello_cobol file is created. This file contains the compiled code and we can run it by: 


$ ./hello_cobol
Output: 
Hello COBOL!

Conclusion: 
Cobol is pretty imperative, procedural language it can be a lot fun.  As a Java programer you may don't want to forget that the parameters in COBOL are mostly passed by the references not by values. Java does manipulated object by references, and all object variables are references but Java doesn't not pass arguments by references, Java passes them by value!
Keep Coding!

No comments: