2.7. Variables

Variables are Qore identifiers prefixed by a "$" sign, similar to Perl. The data type of variables does not have to be declared in advance, and variable types are assigned and changed automatically as appropriate in the context of the operations being performed on them or the values being assigned. Any Qore variable can hold any Qore data type including container types (lists, hashes, or objects).

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 subroutine or method arguments that were not assigned to parameter variables (see Subroutines for more 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

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

2.7.1. Lexical Scope

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 subroutine, method, or catch block. Global variables can be explicitly declared with our. The our keyword is required if the parse option PO_REQUIRE_OUR (-O or --require-our command-line option) is set for the parent program. See the section on Parse Options for more information.

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 $a = 1;        # this is a global variable
our ($b, $c, $d);  # list of global variables

if ($a == 1) {
    my $a = 2; 
    my ($b, $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