Question

I recently got into the IBM's RPG world, so while writing some code and reviewing other people's code I've found that some use Eval and some others use Move to accomplish the same things. Is there any difference between them?

Thanks.

Was it helpful?

Solution

Yes! There is a big difference between these operation codes. The RPG Reference manual is a good place to start.

It's important to understand that RPG is a strongly typed language. A given variable is declared to be a certain type, with a certain size and it is permanent until the program is re-compiled. For character variables, only character operations are allowed. For numeric variables, only numeric operations are allowed. The compiler forbids operations which don't belong to the variable type.

MOVE (and variants MOVEL and MOVEA) are intended to move (copy) bytes around in memory. An intentional side effect of some MOVE operations is to convert between character data type and numeric. It would take a book to describe the myriad uses that MOVEx has, but each and every one is intimately related to the fact that it moves (copies) bytes from this memory location to that memory location.

EVAL has a very different primary function: EVAL is designed to evaluate an expression. Most languages allow an expression like BASICs LET N = X^2 + Y + Z. In old (very old) RPG, there were no expressions. Each line of code did one and only one calculation. One could ADD this number to that, or COMPare this variable to that. If we had to implement the above calculation in (very old) RPG, we would have needed something like:

C   X    MULT X     N
C   N    ADD  Y     N
C   N    ADD  Z     N

With EVAL, we can do this: EVAL N = X*X + Y + Z It is critical to understand that this EVALuation requires the compiler to create some internal work variables in order to hold the intermediate results. The numeric precision of these intermediate fields is important to understand before converting old code willy-nilly. That's because the fixed format code operations MULT and ADD might very well silently truncate, but EVAL will signal an overflow.

Why did I spend so much time talking about expressions and EVAL? Because one of the possible expressions is 'copy this variable to that'. EVAL will NOT convert between data types. If you need to convert, you will need to supply code like %dec() or %editc() to do that.

This is why you will see MOVE used in even recent code; someone preferred to have the compiler do the implicit type conversions with MOVE rather than write explicit expressions to do them with EVAL.

As an aside, even EVAL is dated. The current (7.1) version of RPG allows fully free form specifications. No 'specification type' in column 6, no /free or /end-free: but completely free form file, data and calculation descriptions. Older versions like 5.4 allow free form calculations following a /free statement. I haven't written a fixed form calculation line in 10 years. EVAL is (mostly) optional in any current compiler.

OTHER TIPS

I like Buck's answer, but to explain it a bit differently...

The results of MOVE can only be known by examining the attributes of the source and destination variables; along with having an intimate knowledge of how MOVE works in various scenarios. There's a lot of implicitness to MOVE.

Whereas EVAL works basically one way. You can end up with the same results you had with MOVE, but you'll have to explicitly take advantage of the various built in functions (aka BIFs) such as $dec(), %char(), %subst().

From a maintainability perspective, EVAL wins. Thus the reason IBM left MOVE and it's siblings out of free-format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top