[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual


Previous Contents Index

9.8.24 WRITE Procedure

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.

For More Information:

9.8.25 WRITELN Procedure

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

For More Information:


Chapter 10
Attributes

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.

For More Information:

  • For information on environment-specific issues about attributes (Appendix A)

10.1 Attribute Syntax

The following syntax applies to all HP Pascal attributes:


                             {constant-expression  }
         [ {identifier1 [[ ( {identifier2          } ,... ) ]] },... ]
                             {                     }

identifier1

The name of the attribute.

constant-expression

A compile-time integer expression, represented in this chapter by n, that qualifies several of the HP Pascal attributes.

identifier2

The name of an option available in one of the following instances:
  • With the CHECK, OPTIMIZE, or KEY attributes.
  • With COMMON and PSECT attributes, indicating the name of a storage area.
  • With the GLOBAL, EXTERNAL, WEAK_GLOBAL, and WEAK_EXTERNAL attributes, indicating an external name. If entered as a quoted string, passed to the linker with case unmodified.

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:

  • By appearing in a type definition in a TYPE section; the item is later declared to be of that type.
  • By appearing in the declaration of an item preceding its type.
  • By appearing before the current declaration section.

Note

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:



TYPE  X = ([1] <= [2])..True;

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 attributes of the type can neither conflict with nor duplicate any attributes explicitly stated in the data item's declaration.
  • The type cannot be used anyplace where its accompanying attributes are illegal.

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.

For More Information:

10.2 Attributes

The following sections describe each attribute in alphabetical order.

10.2.1 ALIGN

The ALIGN attribute controls the default alignment, packing, and allocation rules in a compilation unit, TYPE section, or VAR section. For example:


[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:


 [ALIGN(VAX)]
TYPE
   vax_type = Array [1..10] of char;

TYPE
   alpha_type = Array [1..10] of char;

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.

Table 10-1 ALIGN Attribute Keywords
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

1Previous versions of HP Pascal used ALPHA_AXP for this keyword. The NATURAL keyword is now the recommended spelling for the same behavior. The ALPHA_AXP keyword will continue to be recognized for compatibility with old source files.

On OpenVMS VAX systems, when you specify a value of NATURAL, automatic variables are aligned on longword boundaries instead of quadword boundaries. This occurs because the largest allowable alignment for the stack is longword alignment.

For More Information:

10.2.2 ALIGNED

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.

Usage and Default Information:

  • The default alignment of an object depends on its size.
  • The constant expression n must denote an integer. If you omit n, the default is 0, indicating byte alignment.
  • If the expression provided to the ALIGNED attribute is greater than the largest supported alignment on the target platform, the compiler will print a warning message and default to the largest supported value. (Note that ALIGNED(13) is the largest alignment allowed on OpenVMS I64 and OpenVMS Alpha systems and ALIGNED(9) is the largest alignment allowed on OpenVMS VAX systems).
  • An automatic variable cannot have alignment greater than an octaword on OpenVMS I64 systems, or a quadword on OpenVMS Alpha systems, or a longword on OpenVMS VAX systems.
  • The minimum alignment for an object of a structured type is the greatest alignment specified for any of its components.
  • Alignment attributes are illegal on nonstatic types, components of files, and on VARYING OF CHAR strings.
  • The alignment of a formal VAR parameter cannot be greater than the alignment of a corresponding actual parameter, either by default or by means of an alignment attribute. In an array variable passed to a conformant formal parameter, alignment and size attributes are illegal on all dimensions of the actual parameter, except the first, that correspond to the dimensions of the formal parameter.
  • On OpenVMS I64 systems, the base type of a pointer variable passed to the NEW procedure cannot have alignment greater than an octaword. On OpenVMS Alpha and OpenVMS VAX systems, the base type of a pointer variable passed to the NEW procedure cannot have alignment greater than a quadword.
  • If the base type of a pointer variable has a specified alignment, then the base type of a pointer expression assigned to it must have an alignment equal to that of the variable.
  • Pointer types are structurally compatible only if their base types have identical alignment.

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.

For More Information:

  • On automatic and size attribute classes ( Section 10.3)
  • On static and nonstatic types ( Section 2.9)
  • On default alignments (HP Pascal for OpenVMS User Manual)

10.2.3 ASYNCHRONOUS

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:

  • This attribute can be applied to routines and to routine parameters declared in external routines.
  • In the absence of the ASYNCHRONOUS attribute, the compiler assumes that the routine can be activated only by actual calls within the program.
  • All predeclared routines are asynchronous by default.
  • Any routines called from within the block of an asynchronous routine must be local to the asynchronous routine or must themselves be asynchronous, either by default or by an explicit attribute.
  • All nonlocal variables accessed from within the block of an asynchronous routine must be declared VOLATILE or READONLY.
  • If a formal routine parameter is asynchronous, all actual parameters passed to it must also be asynchronous.
  • An asynchronous routine can be passed as an actual parameter to a formal routine parameter that does not have this attribute.

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.

For More Information:


Previous Next Contents Index