LOCAL - declare local variables

SYNOPSIS

<LOCAL var[=value|$initValue] ...]>
  ...
[</LOCAL>]


DESCRIPTION
The LOCAL statement declares variables to be local. Within its block, the named variables will exist as local variables (see Variable Scope discussion, here), and will be destroyed when the block exits. They are distinct from, and hide, other variables of the same name (if any) currently in scope.

Local variables are useful as "scratch space" in functions, without the side effects possible with global variables (i.e. modifying another function's information). Like script function parameters, they can be initialized with a value or default variable upon entry to the block. However, if the default is a variable, it is taken from the scope of the <LOCAL> tag: this means it could be another local variable, unlike function default variables which are necessarily global. LOCAL variables without default values are initialized to empty (no values).

The scope of LOCAL variables ends with a matching </LOCAL> tag. If a </LOCAL> tag is not given, the scope ends with the block enclosing the <LOCAL>.


EXAMPLE

<A NAME=sec2time sec>
<!-- Prints linear seconds value as hours, minutes, seconds -->
  <LOCAL h m s>
  <$h = ($sec / (60 * 60))>
  <$s = ($sec - ($h * 60 * 60))>
  <$m = ($s / 60)>
  <$s = ($s - ($m * 60))>
  The time is $h hours, $m minutes and $s seconds.
</A>

This function uses local variables as scratch space in its computations, thus ensuring that it can't modify the data of another function or global variables.


CAVEATS
The LOCAL statement was added in version 2.6.911900000 19981123.

Local variables are destroyed when their block exits and their values lost. The next time the block is entered, the variables will be re-initialized by the above rules.

A local variable of the same name as another in-scope variable will "hide" the outer variable from visibility.


SEE ALSO
Function definitions, Variable scope discussion


Copyright © Thunderstone Software     Last updated: Oct 24 2023
Copyright © 2024 Thunderstone Software LLC. All rights reserved.