[Info-vax] puzzled by DCL symbol substitution
AEF
spamsink2001 at yahoo.com
Sat Jan 7 08:36:44 EST 2012
On Jan 7, 7:19 am, hel... at astro.multiCLOTHESvax.de (Phillip Helbig---
undress to reply) wrote:
> After 20 years of VMS, I thought I understand everything, despite a)
> unclear examples (e.g. uncleard distinction between to apostrophes
> belonging to different symbols and a double apostrophe; the distinction
> is not always clear (to the reader, though it is to DCL) in complicated
> expressions) and b) some obvious typos in the documentation. In
> particular, I'm referring to the three phases of processing, the order
> of symbol substitution etc. I'm surprised that the following works:
>
> $ SAVE_VERIFY = 'F$VERIFY(0)'
> $ VERIFY_PROCEDURE = "''F$PARSE(F$ENVIRONMENT("PROCEDURE"),,,"NAME")'_VERIFY"
> $ VERIFY_PROCEDURE = "''&VERIFY_PROCEDURE'"
> $ IF (SAVE_VERIFY .OR. VERIFY_PROCEDURE) THEN SET VERIFY
> [code which should be displayed if any verification is set]
> $ VERIFY_PROCEDURE = 0*'F$VERIFY(VERIFY_PROCEDURE)' + VERIFY_PROCEDURE
> [code which should be displayed if only the special verification is set]
> $ EXIT $STATUS + 0*F$VERIFY(SAVE_VERIFY)
>
[...]
> The third line is magic.
>
[...]
> Why does the third line work? The LOGIC is clear: the symbol
> VERIFY_PROCEDURE gets expanded to the name of the symbol appropriate for
> this procedure, then its value is evaluated. This construction (and
> none other of which I am aware) works if the value is 1, 0 (or TRUE,
> FALSE etc) or if it is undefined. However, my impression was that a)
> apostrophes are evaluated before ampersands and b) ampersands are not
> evaluated inside quotes. So why does it work?
It works because the apostrophes force ampersand substitution to take
place.
>
> Or does the following happen? Here's the line again
>
> $ VERIFY_PROCEDURE = "''&VERIFY_PROCEDURE'"
>
> First, VERIFY_PROCEDURE is set to the string value
> "''&VERIFY_PROCEDURE'"; no substitution happens since &VERIFY_PROCEDURE
> is not the name of a symbol. Second, the ampersand is evaluated,
> resulting in the string "''TEST_VERIFY'" (if the procedure is called
> TEST.COM). The following line then becomes
>
> $ IF (SAVE_VERIFY .OR. "''TEST_VERIFY'") THEN SET VERIFY
>
> Then, of course, "''TEST_VERIFY'" is evaluated to its value if it has
> one or to an empty string if it is not defined.
>
> Is this really what happens?
No.
> If it is, and you know that it is, did you
> know before reading my guess about what happens?
Yes. I've been through all this before and made many posts to this
newsgroup about it.
The apostrophes force the ampersand to act.
> $ VERIFY_PROCEDURE = "''F$PARSE(F$ENVIRONMENT("PROCEDURE"),,,"NAME")'_VERIFY"
> $ VERIFY_PROCEDURE = "''&VERIFY_PROCEDURE'"
The second line is "magic", too. Quotation marks normally have to be
doubled inside a quoted string, but this is the exception. The
quotation marks around PROCEDURE and NAME must *not* be doubled. For
some reason it's different when lexical functions are used in this
manner.
In the third line:
Ampersand changes VERIFY_PROCEDURE to its value. Then the apostrophes
change that result to *its* value. Then the resultant string is
assigned to VERIFY_PROCEDURE.
Example:
$ ZERO = "ONE"
$ ONE = "TWO"
$ TWO = "THREE"
$
$ A = "''ZERO'"
$ SH SYM A
A = "ONE"
$
$ A = "''&ZERO'"
$ SH SYM A
A = "TWO"
$
$ A = "&ZERO"
$ SH SYM A
A = "&ZERO"
$
As you can see in the second case, the symbol "A" gets its value
before any subsequent code is executed.
>
> Does anyone know who invented this?
The authors of DCL.
No, this is not documented in the manuals, but that's how it works. On
the other hand, isn't this just like the apostrophes forcing the
lexical function to be evaluated in line 2 or your procedure? It is.
It's the same thing. Lexical substitution is normally done during
phase 3, but the apostrophes force it to be carried out in step 1.
AEF
More information about the Info-vax
mailing list