Assignment Commands


The assignment command is the means by which DCL symbols are created and assigned values. Because most languages refer to their commands as statements, the assignment command is often called assignment statement. The assignment statement has the following general format:

     $ symbol = expression 

The symbol named on the left-hand side of the equal sign is assigned the value determined by the expression on the right-hand side. For example:

     $ my_name = "Brian Cutler" 

Here the symbol MY_NAME is assigned the character string "Brian Cutler". We read the assignment statement as "MY_NAME gets "Brian Cutler"" and say that the symbol MY_NAME is set to the value of the expression "Brian Cutler". The word equals is not used when discussing the action of an assignment statement because it is too easily confused with the idea of equality for comparison purposes, as in "1 does not equal 2." Integer and string literals are examples of simple expressions; a literal can appear by itself on the righthand side of an assignment statement. Section 3.4 describes expressions in detail.

A symbol is created the first time a command is used to assign it a value. Subsequent assignments to the same symbol discard the current value and replace it with the new value. A symbol can have an integer value at one point in the program and a character string value at another point. The data type is associated with the value, not with the symbol, as it is in many languages such as Pascal. Because the data type is associated with the value and not with the symbol, a symbol can be set to any type of data. The following assignment statements illustrate these rules:

     $ days_per_year = 366     $ days_per_leap_year = days_per_year     $ days_per_year = 365     $ days_per_leap_year = "three hundred sixty-six" 

In the second assignment statement, the symbol DAYS_PER_LEAP_YEAR is set to the value of the previously created symbol DAYS_PER_YEAR. A symbol by itself is another example of a simple expression on the right-hand side of an assignment command. Note that DAYS_PER_LEAP_YEAR is assigned both types of data at different times. In addition to its name and value, each symbol has a level. The name and the level together uniquely identify the symbol from among all existing symbols. A symbol's level is determined by the context in which it is created and by the kind of assignment statement used to create it. The following sections describe the different levels: prompt level, procedure level, and global level.

DCL Prompt Level

Symbols created at the DCL prompt are associated with the DCL prompt level. You might create such a symbol to remember something to do later:

    $ note = "Remember to call Jim before leaving." 

This symbol is at the prompt level because it was created with an assignment statement at the DCL prompt. You can display the value of the symbol with the SHOW SYMBOL command (you type the first line, and OpenVMS responds with the second line):

    $ show symbol note    NOTE = "Remember to call Jim before leaving." 

The symbol can be used to create another symbol at the prompt level:

    $ old_note = note    $ note = "Don't forget to pick up the video tape" 

Now two prompt-level symbols exist, NOTE and OLD_NOTE. Symbols at the prompt level remain in existence during your entire login period unless explicitly deleted.

Procedure Level

Each DCL command procedure has its own symbol level. When you invoke a procedure with the at-sign (@) command, a new level is established. Any symbols created by the procedure are associated with the procedure's level. If the procedure invokes another procedure, the second procedure has its own level and its symbols are associated with its level. In this way, each procedure has a set of symbols that "belong" to it.

Here is a simple procedure named SIMPLE-PROC:

    $ name = "OpenVMS is VMS"    $ show symbol name 

When invoked, this procedure creates the procedure-level symbol NAME and assigns a character string to it. The procedure then displays the value of the symbol:

    $ @simple-proc    NAME = "OpenVMS is VMS" 

When a procedure terminates, all the symbols created at its level are deleted. Since the symbols are associated with the procedure's level, it makes no sense for them to exist once the procedure has terminated. Should the procedure be invoked again, its symbols will be recreated from scratch by DCL; they will have lost their old values.

A procedure can refer to a symbol at the prompt level as long as the symbol does not have the same name as a symbol created by the procedure. If symbols with the same name are created at the prompt and procedure levels, the one at procedure level "shadows" or "hides" the one at prompt level. Similarly, a subprocedure invoked froma main procedure can refer to symbols created by the main procedure. For example, if procedure A invokes procedure B, B can refer to the symbols created by A. The ability to use symbols created at an outer level allows you to write a procedure that displays the NOTE symbol created at the prompt level:

    $! Procedure to display the note.    $    $ write sys$output "Your note is:"    $ show symbol note 

The WRITE command displays the text in double quotes. The SHOW SYMBOL command refers to the NOTE symbol created at the prompt level. Because this little procedure does not create its own symbol named NOTE, the reference to NOTE gets the symbol at prompt level. The display produced by this procedure is shown here:

    Your note is:    Note = "Don't forget to pick up the video tape." 

When DCL is executing a procedure and needs the value of a symbol, it performs a simple search process. As soon as it finds a symbol with the given name, it terminates the search and uses the value of the symbol. Here is the process as described so far:

  1. DCL looks for the symbol among the symbols created by the currently executing procedure.

  2. If the procedure was invoked by another procedure, DCL looks for the symbol among the invoking procedure's symbols.

  3. Step 2 is repeated for each additional level of command procedure

  4. DCL looks for the symbol among the symbols created at DCL prompt level.

Although a procedure can use the values of symbols at outer levels, it cannot create or change symbols at outer levels. This is somewhat restrictive, so a special global level exists to solve the problem.

Global Level

Sophisticated DCL applications are composed of multiple procedures, which invoke one another in various combinations. Sometimes it is necessary for a procedure B to calculate a value and pass it back to another procedure A, which invoked it. With what you know so far, there is no way to accomplish this. If B stores the value in a symbol at its own level, that symbol will be deleted when B terminates. The alternative is for B to store the value in a symbol at A's level, but DCL provides no means of doing so. The global symbol level exists to solve this dilemma.

Symbols created at the global level are called global symbols. You must explicitly request that a global symbol be created by using a variant of the assignment statement:

     $ xda_answer == 42 

Note that two equal signs are used in the example. The double equal sign requests that the symbol XDA_ANSWER be created at the global level and assigned the value 42. Global symbols are only created when you use the double equal sign form of the assignment command.

By convention, global symbol names begin with the application facility code and a single underscore. In the preceding example, the facility code is XDA.

Global symbols can always be created, whether you are at the DCL prompt or executing a command procedure. This capability is what distinguishes global symbols from prompt-level symbols and is the only major difference between the two kinds of symbols. The double equal sign forces a global symbol to be created regardless of the level at which the assignment statement is executed. Subsequent global assignment to the same symbol changes the global symbol's value. The value of a global symbol can be obtained at any level as long as there are no symbols of the same name at a procedure level or prompt level. To accommodate global symbols, the symbol search process is extended to its final form:

  1. DCL looks for the symbol among the symbols created by the currently executing procedure.

  2. If the procedure was invoked by another procedure, DCL looks for the symbol among the invoking procedure's symbols.

  3. Step 2 is repeated for each additional level of command procedure.

  4. DCL looks for the symbol among the symbols created at prompt level.

  5. DCL looks for the symbol among the global symbols. The following simple example illustrates a subprocedure that creates a global symbol in order to pass a value back to its calling procedure:

     $! This is procedure A $ $ @b ! B will set global symbol XDA_ANSWER $! This is procedure B $ $ xda_answer == 42 $ exit 

Procedure A invokes procedure B to establish the global value. Procedure B creates the global symbol XDA_ANSWER using a double equal sign assignment statement. It then exits, allowing procedure A to continue. Procedure A displays the value of the global symbol. Because XDA_ANSWER is global, it is not deleted when procedure B exits, thus allowing A to obtain its value. It is quite easy to omit the second equal sign when you mean to perform a global assignment. Look at the following code:

    $ xda_answer == 42    .    .    .    $ xda_answer = 43 ! Meant to use ==    $ show symbol xda_answer 

The first assignment command creates the global symbol XDA_ANSWER and assigns it the value 42. The second assignment command was intended to change the value of the global symbol, but only one equal sign is present. Therefore, the assignment command creates a procedure-level symbol with the same name, XDA_ANSWER. This procedure-level symbol hides the global symbol. The SHOW command displays 43 quite nicely, but the global symbol still has the value 42. Be careful always to use two equal signs when performing global assignments.

Global symbols remain in existence during your entire login period, unless explicitly deleted.

Symbol Scope

Symbol scoping allows a cautiously written, modular command procedure to avoid unintended and undesirable interactions involving symbols. Scoping does not delete any existing symbols; it controls the visibility of symbols in the current procedure level.

You can control symbol scoping using the SET SCOPE command. When SET SCOPE is called at a procedure level, it can hide the global symbols, the local symbols created at any outer procedure levels, or both, from the current procedure scope. SET SCOPE can also be used avoid problems where one or more symbols might conflict with DCL command verbs.

When the procedure level that called SET SCOPE exits, the scoping rules for the outer level are restored.




Getting Started with OpenVMS System Management
Getting Started with OpenVMS System Management (HP Technologies)
ISBN: 1555582818
EAN: 2147483647
Year: 2004
Pages: 130
Authors: David Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net