2.8. Variables

Unless parse option %allow-bare-refs is set, variables are Qore identifiers prefixed by a "$" sign, similar to Perl. If a variable is declared without any type restriction, then it is assumed to have type any. In this case, variables so declared can hold any data type.

A few variables are set by the Qore language during the execution of Qore programs. These are normal variables that can be reassigned to other values by the user if necessary.

Table 2.5. Special Qore Variables

Variable

Type

Data Type

Explanation

$argv

Local

List

automatically assigned local variable containing the list of function or method arguments that were not assigned to parameter variables (see Functions and Implicit Arguments for supporting related information)

$ARGV

Global

List

script command-line arguments (use the GetOpt Class to parse command-line arguments)

$QORE_ARGV

Global

List

complete qore command-line arguments

$ENV

Global

Hash

UNIX program environment


Note

As of version 0.5.0, $STDERR and $STDOUT have been removed from Qore. Use the I/O constantsstderr, stdout, and stdin constants of the File Class instead.

2.8.1. Variable Declarations and Lexical Scope

Unless the %assume-local parse directive is used, variables not in a parameter list automatically have global scope unless the first reference is prefixed with my. Variable names in a parameter list are always local to their associated function, method, or catch block. Global variables can be explicitly declared with our. The our keyword is required if the parse option %require-our (-O or --require-our command-line option) is set for the parent program. See the section on Parse Options for more information.

When the %assume-local parse directive is used, variables without an explicit scope declaration (i.e. my or our) are assumed to be local variables.

Variables may be assigned any value unless restricted with a type declaration. If no type declaration is given, then the variable is assumed to be type any. Note that type declarations are required for all variables (and for function and method parameters and class members) when the %require-types parse option is set.

Local variables are not shared between threads (local variables have a distinct value in each thread), however global variables are. See Threading (and in particular Threading and Variables) for more information.

For example (in the following script, the our keyword is optional):

#!/usr/bin/qore
#
# variable scoping example

our int $a = 1;                    # this is a global variable
our (string $b, any $c, hash $d);  # list of global variables

if ($a == 1) {
    my int $a = 2; 
    my (string $b, any $c);
    # $a, $b, and $c are local variables, 
    # the use of which will not affect the 
    # global variables of the same name
    print("local a = %d\n", $a); 
}

print("global a = %d\n", $a); 

The first print() statement will output:

local a = 2

The second print() statement will output:

global a = 1

Note

If parse option %allow-bare-refs is set, then variable references must be made without the "$" character.

There is 1 comment
May 05, 2011 - 18:33

I feel so much happier now I understand all this. Thnaks!

Leave a Comment



?
? ?
?

 
Powered by TalkBack