[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP Pascal for OpenVMS
|
Previous | Contents | Index |
The WRITE procedure assigns data to an output file.
WRITE([[file_variable, ]]{expression},... [[, ERROR := error-recovery]] |
file_variable
The name of the file variable associated with the output file. If you omit the name of the file, the default is OUTPUT.expression
An expression whose value is to be written; multiple output values must be separated with commas. An output value must have the same type as the file components; however, values written to a TEXT file can also be expressions of any ordinal, real, or string type. You can specify the output format of the expression as described in Section 9.6.error-recovery
The action to be taken if an error occurs during execution of the routine.
The file ( unless it is a random-access by key file ) must be in generation mode before WRITE is called; it remains in that mode after WRITE has executed.
By definition, a WRITE statement to a nontext file performs an assignment to the file buffer variable and a PUT statement for each output value. For nontext files, the types of the output values must be assignment compatible with the component type of the file. For example:
WRITE( file_variable, expression ); |
This procedure call is similar to the following example:
file_variable^ := expression; PUT( file_variable ); |
For TEXT files, the WRITE procedure converts the value of each expression to a sequence of characters. It repeats the assignment and PUT process until all the values have been written to the file.
Consider the following example:
TYPE String = PACKED ARRAY[1..20] OF CHAR; VAR Names : FILE OF String; Pres : String; {In the executable section:} WRITE (Names, 'Millard Fillmore ', Pres); |
This example writes two components in the file Names. The first is the 20-character string constant 'Millard Fillmore '. The second is the value of the string variable Pres.
The WRITELN procedure writes a line of data to a text file.
WRITELN [[( [[file_variable,]] {expression,... [[, ERROR := error-recovery]] )]] |
file_variable
The name of the file variable associated with the text file to be written. If you omit the name of the file, the default is OUTPUT.expression
An expression whose value is to be written; multiple output values must be separated by commas. The expressions can be of any ordinal, real, or string type and are written with a default field width, which you can override as described in Section 9.6.error-recovery
The action to be taken if an error occurs during execution of the routine.
The file must be in generation mode before WRITELN is called; it remains in that mode after WRITELN has been executed.
The WRITELN procedure writes the specified values into the TEXT file, inserts an end-of-line marker after the end of the current line, and then positions the file at the beginning of the next line. When applied to several expressions, WRITELN performs the following sequence:
WRITE (file_variable, {expression},...); WRITELN (file_variable); |
Consider the following example:
WRITELN( User_Guide, 'This manual describes how to interact'); |
This procedure writes the string to the TEXT file User_Guide, follows
it with an end-of-line marker, and skips to the next line.
You can specify a carriage-control character as the first item in an
output line. When you use carriage-control characters, make sure that
the file is open with either the CARRIAGE or FORTRAN option. Consider
the following example:
WRITELN( Tree, ' ', String1, String2 ); |
The first item in the list is a space character. The space indicates that the values of String1 and String2 are printed on a new line when the file is written to a terminal, line printer, or similar carriage-control device.
If you specify a carriage format but use an invalid carriage-control character, the first character in the line is ignored and the output appears with the first character truncated. Consider the following example:
TYPE A_String = PACKED ARRAY[1..25] OF CHAR; VAR New_Hires : TEXT; n : INTEGER; New_Rec : RECORD Id : INTEGER; Name, Address : A_String; END; {In the executable section:} OPEN( New_Hires, 'new_hires.dat', CARRIAGE_CONTROL := FORTRAN ); REWRITE( New_Hires ); WITH New_Rec DO BEGIN WRITELN( New_Hires, '1New hire # ', ID:1, ' is ', Name ); WRITELN( New_Hires, ' ', Name, 'lives at:' ); WRITELN( New_Hires, ' ' ); WRITELN( New_Hires, ' ', Address ); END; |
In this example, four lines are written to the TEXT file New_Hires. The
output starts at the top of a new page, as directed by the
carriage-control character '1', and appears in the following format:
New hire # 73 is Irving Washington Irving Washington lives at: 22 Chestnut St, Seattle |
An attribute is an identifier that directs the HP Pascal compiler to change its behavior in some way. This chapter discusses the following information about attributes:
When an attribute is not explicitly stated, the compiler follows the default rules to assign properties to program elements. However, using attributes to override the defaults allows additional control over the properties of data items, routines, and compilation units.
For convenience in description, the attributes are grouped in attribute classes. All attributes in a given class share common characteristics (sometimes there is only one attribute to a class).
Table 10-7 presents the attributes in each class. To save you the time of scrolling through all the attributes, you can click on the Table Of Contents entry for the desired attribute.
The following syntax applies to all HP Pascal attributes:
{constant-expression } [ {identifier1 [[ ( {identifier2 } ,... ) ]] },... ] { } |
The name of the attribute.identifier1
A list of attributes can appear anywhere in the VAR, TYPE, and CONST declaration sections, and anywhere in a program that a type, a type identifier, or the heading of a routine or compilation unit is legal. However, only one attribute from a particular class can appear in a given attribute list. The use of attribute lists is shown in examples throughout this chapter. The names of attributes, when used in a suitable context, cannot conflict with other identifiers with the same name in the program.
Syntactically, an attribute list can appear before a VAR, TYPE, and CONST section in the declaration section. In this case, the attributes would apply to all elements in that particular section. However, HP Pascal only allows you to use the ALIGN, ENUMERATION_SIZE, and HIDDEN attribute in this way.
Some attributes require a special form of constant expression called a
name string. The syntax of a name string differs from
that of other strings in
HP Pascal only in that a name string cannot use the extended-string
syntax.
Every program element must be associated with one property for each applicable attribute class. The HP Pascal compiler automatically supplies the defaults for the unspecified classes at the time of the element's declaration. In some classes, as described in the following sections, the default property is not available through an explicit attribute.
Attributes can be associated with data items in the following ways:
In HP Pascal, the presence of constant expressions and attribute lists leads to a minor ambiguity in the language syntax. If the compiler finds a left bracket ([) symbol when it expects to find a type or type identifier, it always assumes that the bracket indicates the beginning of an attribute list. The ambiguity arises because the left bracket could also represent the beginning of a set constructor that denotes the low bound of a subrange type. If the latter case is in fact what you intend, enclose the set constructor in parentheses; the compiler will interpret the expression correctly. For example:
|
When a type definition includes a list of attributes, the type has only those attributes specified. The compiler does not supply the defaults for the unspecified classes until a data item is declared to be of that type. Two rules govern the use of attributes in a TYPE section:
The following example shows both legal and illegal use of attributes in type definitions:
TYPE A = [GLOBAL] INTEGER; B = [UNALIGNED] INTEGER; VAR A1 : [GLOBAL] A; { Illegal; duplicates GLOBAL attribute of type A } A2 : [EXTERNAL] A; { Illegal; conflicts with GLOBAL attribute of type A } B1 : ^B; { Illegal; pointer base type cannot be UNALIGNED } C : A; { Legal } |
The first three variable declarations are illegal for the reasons shown in the comments. The declaration of C is legal; C is declared as a global INTEGER because of the characteristics of its type. The compiler supplies defaults for all other classes applicable to the variable C.
Attributes associated with data items usually modify type compatibility rules. These modifications are explained in the sections describing individual attributes.
The following sections describe each attribute in alphabetical order.
The ALIGN attribute controls the default alignment, packing, and
allocation rules in a compilation unit, TYPE section, or VAR section.
For example:
10.2.1 ALIGN
[ALIGN(keyword)] |
The ALIGN attribute takes a single keyword parameter that has the same
name and meaning as the keywords for the /ALIGN qualifier. However,
specifying the ALIGN attribute overrides any value that you previously
specified using the /ALIGN qualifier.
The ALIGN attribute can appear at the beginning of the compilation unit
and before a TYPE or VAR section. When used before a TYPE or VAR
section, the default alignment, packing, and allocation rules are
modified only for the duration of the TYPE or VAR section. For
example:
Usage and Default Information:
Table 10-1 lists the keywords for the ALIGN attribute. See
Section A.2.7 for a complete description of the alignment rules for
each platform and Section A.2.4 for a complete description of the
allocated sizes for objects for each platform.
[ALIGN(VAX)]
TYPE
vax_type = Array [1..10] of char;
TYPE
alpha_type = Array [1..10] of char;
Keyword | Action | Default on System |
---|---|---|
NATURAL 1 | Uses natural alignment when positioning record fields or array components. Natural alignment is when a field or component is positioned on a boundary based on its size. For example, 32-bit integers would be aligned on the nearest 32-bit boundary. |
OpenVMS I64 and
OpenVMS Alpha |
VAX | Uses byte alignment when positioning record fields or array components. Field or components larger than 32 bits are positioned on the nearest byte boundary. | OpenVMS VAX |
The ALIGNED attribute indicates the object is to be aligned on a specific memory boundary.
ALIGNED [[( n )]] |
An aligned object is aligned on the memory boundary indicated by n. The
constant expression n indicates that the address of the object must end
in at least n zeros. ALIGNED( 0 ) specifies byte alignment, ALIGNED( 1
) specifies word alignment, ALIGNED( 2 ) specifies longword alignment,
ALIGNED( 3 ) specifies quadword alignment, ALIGNED( 4 ) specifies
octaword alignment, and ALIGNED( 9 ) specifies alignment on a 512-byte
boundary.
The following is an example of the ALIGNED attribute:
VAR Free_Buffers : [ ALIGNED( 1 ), WORD] -2**15..2**15-1; {In the executable section:} IF ADD_INTERLOCKED( -1, Free_Buffers ) <= 0 THEN {Statement:} |
The predeclared function ADD_INTERLOCKED requires that the second parameter passed to it have word alignment and an allocation size of one word. In this example, the variable Free_Buffers is declared with alignment and size attributes to meet these restrictions.
The ASYNCHRONOUS attribute indicates that a routine can be called asynchronously to the main program execution. For example, AST routines or condition handlers are asynchronous routines because they are called without an explicit procedure call in your program. Routines with the ASYNCHRONOUS attribute can only access local variables and up-level VOLATILE variables. Additionally, asynchronous routines can only call other asynchronous routines.
Usage and Default Information:
Consider the following example:
PROCEDURE Do_Something; VAR i : [VOLATILE] INTEGER; j : INTEGER [ASYNCHRONOUS] FUNCTION Handler {Two array parameters} : BOOLEAN; BEGIN i := i + 1; {Remaining function body...} {In the executable section of the procedure:} ESTABLISH( Handler ); |
This example shows the declaration of the asynchronous function Handler. The executable section of Handler cannot access variables declared in the enclosing block of the procedure Do_Something unless those variables are declared VOLATILE. Handler can access the variable i, which has the VOLATILE attribute, but cannot access the variable j.
Previous | Next | Contents | Index |