Unless parse option \mbox{\hyperlink{parse_directives_allow-bare-refs}{\%allow-\/bare-\/refs}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} are set, variables are Qore identifiers prefixed by a {\ttfamily \char`\"{}\$\char`\"{}} sign, similar to \href{http://www.perl.org}{\texttt{ Perl}}. If a variable is declared without any type restriction, then it is assumed to have type \mbox{\hyperlink{data_type_declarations_any_type}{any}}. In this case, variables so declared can hold any data type.\hypertarget{variables_special_variables}{}\doxysection{Special Variables}\label{variables_special_variables} 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. {\bfseries{Special Qore Variables}} \tabulinesep=1mm \begin{longtabu}spread 0pt [c]{*{4}{|X[-1]}|} \hline {\bfseries{Variable}} &{\bfseries{Scope}} &{\bfseries{Data Type}} &{\bfseries{Explanation}} \\\cline{1-4} {\ttfamily argv} &Local &\mbox{\hyperlink{data_type_declarations_list_or_nothing_type}{$\ast$list}} &automatically assigned local variable containing the list of function or method arguments that were not assigned to parameter variables (see \mbox{\hyperlink{expressions_implicit_arguments}{Implicit Argument References}} for more information) \\\cline{1-4} {\ttfamily Qore\+::\+ARGV} &Global &\mbox{\hyperlink{data_type_declarations_list_type}{list}} &script command-\/line arguments (use the \mbox{\hyperlink{class_qore_1_1_get_opt}{Qore\+::\+Get\+Opt}} class to parse command-\/line arguments) \\\cline{1-4} {\ttfamily Qore\+::\+QORE\+\_\+\+ARGV} &Global &\mbox{\hyperlink{data_type_declarations_list_type}{list}} &complete qore command-\/line arguments \\\cline{1-4} {\ttfamily Qore\+::\+ENV} &Global &\mbox{\hyperlink{data_type_declarations_hash_type}{hash}} &UNIX program environment \\\cline{1-4} \end{longtabu} \begin{DoxyNote}{Note} global variables are namespace members; if a namespace path is not declared for the global variable, then the global variable will reside in the root namespace \end{DoxyNote} \hypertarget{variables_variable_declarations}{}\doxysection{Variable Declarations and Lexical Scope}\label{variables_variable_declarations} Unless the \mbox{\hyperlink{parse_directives_assume-local}{\%assume-\/local}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} parse directives are used, variables not in a parameter list automatically have global scope unless the first reference is prefixed with \mbox{\hyperlink{variables_my}{The \char`\"{}my\char`\"{} Keyword}}. Variable names in a parameter list are always local to their associated function, method, or catch block. \mbox{\hyperlink{variables_global_variables}{Global variables}} can be explicitly declared with \mbox{\hyperlink{variables_our}{The \char`\"{}our\char`\"{} Keyword}}. The \mbox{\hyperlink{variables_our}{The \char`\"{}our\char`\"{} Keyword}} keyword is required if the parse option \mbox{\hyperlink{parse_directives_require-our}{\%require-\/our}} ({\ttfamily -\/O} or {\ttfamily --require-\/our} command-\/line option) is set for the parent program. See \mbox{\hyperlink{group__parse__options}{Parse Option Constants}} for more information. When the \mbox{\hyperlink{parse_directives_assume-local}{\%assume-\/local}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} parse directive is used, variables without an explicit scope declaration (i.\+e. \mbox{\hyperlink{variables_my}{The \char`\"{}my\char`\"{} Keyword}} or \mbox{\hyperlink{variables_our}{The \char`\"{}our\char`\"{} Keyword}}) are assumed to be \mbox{\hyperlink{variables_local_variables}{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 \mbox{\hyperlink{data_type_declarations_any_type}{any}}. Note that type declarations are required for all variables (and for function and method parameters and class members) when the \mbox{\hyperlink{parse_directives_require-types}{\%require-\/types}} parse option is set. \mbox{\hyperlink{variables_local_variables}{Local variables}} are not shared between threads (local variables have a distinct value in each thread, with some very particular exceptions), however global variables are. See \mbox{\hyperlink{threading}{Threading}} (and in particular \mbox{\hyperlink{threading_threading_and_variables}{Threading and Variables}}) for more information. Global variables are members of \mbox{\hyperlink{qore_namespaces}{namespaces}}; if a global variable is declared anywhere outside a namespace declaration, and a namespace path is not given, then the global variable will be assumed to be in the root namespace. For example (in the following script, the \mbox{\hyperlink{variables_our}{The \char`\"{}our\char`\"{} Keyword}} keyword is optional unless \mbox{\hyperlink{parse_directives_require-our}{\%require-\/our}} is set)\+: \begin{DoxyCode}{0} \DoxyCodeLine{\textcolor{comment}{\#!/usr/bin/qore}} \DoxyCodeLine{\textcolor{comment}{\#}} \DoxyCodeLine{\textcolor{comment}{\# variable scoping example}} \DoxyCodeLine{\%new-\/style} \DoxyCodeLine{} \DoxyCodeLine{our int a = 1; \textcolor{comment}{\# this is a global variable}} \DoxyCodeLine{our (string b, any c, hash d); \textcolor{comment}{\# list of global variables}} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{keywordflow}{if} (a == 1) \{} \DoxyCodeLine{ int a = 2;} \DoxyCodeLine{ my (string b, any c);} \DoxyCodeLine{ \textcolor{comment}{\# a, b, and c are local variables,}} \DoxyCodeLine{ \textcolor{comment}{\# the use of which will not affect the}} \DoxyCodeLine{ \textcolor{comment}{\# global variables of the same name}} \DoxyCodeLine{ \mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf}}(\textcolor{stringliteral}{"{}local a = \%d\(\backslash\)n"{}}, a);} \DoxyCodeLine{\}} \DoxyCodeLine{} \DoxyCodeLine{\mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf}}(\textcolor{stringliteral}{"{}global a = \%d\(\backslash\)n"{}}, a);} \end{DoxyCode} The first \mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf()}} statement will output\+: \begin{DoxyVerb}local a = 2\end{DoxyVerb} The second \mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf()}} statement will output\+: \begin{DoxyVerb}global a = 1\end{DoxyVerb} \begin{DoxyNote}{Note} If parse option \mbox{\hyperlink{parse_directives_allow-bare-refs}{\%allow-\/bare-\/refs}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} is set, then variable references {\bfseries{must}} be made without the \char`\"{}\$\char`\"{} character. Because Qore is moving toward establishing \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} as the default syntax; all examples given in the Qore documentation are given assuming \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} unless otherwise indicated. \end{DoxyNote} \hypertarget{variables_variable_implicit_construction}{}\doxysubsection{Variable Implicit Construction}\label{variables_variable_implicit_construction} Variables with supported type declarations can be instantiated with a special syntax as follows\+: \mbox{[}\mbox{\hyperlink{variables_my}{my}}$\vert$\mbox{\hyperlink{variables_our}{our}}\mbox{]} {\itshape \mbox{\hyperlink{data_type_declarations}{var\+\_\+type}} var\+\_\+name}{\ttfamily (}\mbox{[}{\itshape \mbox{\hyperlink{expressions}{arg\+\_\+expression}}}{\ttfamily , ...\mbox{]});} This allows a variable to be declared with a specific type and initialized at the same time. The following types can be used for {\itshape \mbox{\hyperlink{data_type_declarations}{var\+\_\+type}}}\+: \begin{DoxyItemize} \item a \mbox{\hyperlink{qore_classes}{class name}} or {\ttfamily \mbox{\hyperlink{data_type_declarations_object_type}{object}}$<$}{\itshape \mbox{\hyperlink{qore_classes}{class\+\_\+name}}}{\ttfamily $>$} for object instantiation (the class name may have a namespace-\/prefixed path) \item {\ttfamily \mbox{\hyperlink{data_type_declarations_hash_type}{hash}}$<$}{\itshape \mbox{\hyperlink{hashdecl}{hashdecl\+\_\+name}}}{\ttfamily $>$} for \mbox{\hyperlink{hashdecl}{type-\/safe hash}} instantiation (the type-\/safe hash name may have a namespace-\/prefixed path) \item {\ttfamily \mbox{\hyperlink{data_type_declarations_hash_type}{hash}}$<$string, }{\itshape \mbox{\hyperlink{data_type_declarations}{type}}}{\ttfamily $>$} for \mbox{\hyperlink{data_type_declarations_hash_complex_type}{complex hash}} instantiation with type-\/safe keys \item {\ttfamily \mbox{\hyperlink{data_type_declarations_list_type}{list}}$<$}{\itshape \mbox{\hyperlink{data_type_declarations}{type}}}{\ttfamily $>$} for \mbox{\hyperlink{data_type_declarations_list_complex_type}{complex list}} instantiation with type-\/safe elements \end{DoxyItemize} \begin{DoxyParagraph}{Examples} \begin{DoxyCode}{0} \DoxyCodeLine{\textcolor{comment}{\# simple object declaration and instantiation}} \DoxyCodeLine{Mutex m();} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# equivalent to the example above}} \DoxyCodeLine{object m();} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# type-\/safe hashdecl hash declaration and instantiation}} \DoxyCodeLine{hash h1((\textcolor{stringliteral}{"{}a"{}}: 1));} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# complex hash with type-\/safe keys declaration and instantiation}} \DoxyCodeLine{hash h2((\textcolor{stringliteral}{"{}a"{}}: 1));} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# complex list with type-\/safe elements declaration and instantiation}} \DoxyCodeLine{list l1(1);} \end{DoxyCode} \end{DoxyParagraph} This provides type information to the parser which allows more errors to be caught at parse time (instead of at run time), and furthermore allows Qore to improve performance by performing more work at parse time rather than for every time the object is accessed at run time (for example, method and variant resolution), and normally requires less typing. This is preferred to using the \mbox{\hyperlink{operators_new}{new operator}} with a variable declaration. \begin{DoxySeeAlso}{See also} \begin{DoxyItemize} \item \mbox{\hyperlink{operators_new}{New Value Operator (new)}} \item \mbox{\hyperlink{operators_cast}{Cast Operator (cast$<$$>$())}} \end{DoxyItemize} \end{DoxySeeAlso} \hypertarget{variables_local_variables}{}\doxysection{Local Variables}\label{variables_local_variables} Local variables are local to the block in which they are declared; they are also thread-\/local, meaning that each thread will have its own value of a local variable. Local variables are generally accessed without any mutual-\/exclusion locking (with the exception of local variables bound in \mbox{\hyperlink{expressions_closure}{closures}} and local variables that have a \mbox{\hyperlink{lvalue_references}{reference}} taken of them). \begin{DoxyNote}{Note} Declaring a variable with {\bfseries{my}} at the top-\/level of a program creates a local variable with global scope; in effect this is a global thread-\/local variable; see \mbox{\hyperlink{threading_threading_and_variables}{Threading and Variables}} for more information. \end{DoxyNote} \hypertarget{variables_my}{}\doxysubsection{The \char`\"{}my\char`\"{} Keyword}\label{variables_my} Variables declared with {\bfseries{my}} are always local variables. {\bfseries{my Example}}~\newline \begin{DoxyCode}{0} \DoxyCodeLine{my int i = 1;} \end{DoxyCode} The {\bfseries{my}} keyword is not required if the \mbox{\hyperlink{parse_directives_assume-local}{\%assume-\/local}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} parse directive is set. In this case, all variables are assumed to be local unless explicitly declared with \mbox{\hyperlink{variables_our}{our}}.\hypertarget{variables_global_variables}{}\doxysection{Global Variables}\label{variables_global_variables} \hypertarget{variables_our}{}\doxysubsection{The \char`\"{}our\char`\"{} Keyword}\label{variables_our} Variables declared with {\bfseries{our}} have global scope and are subject to mutual exclusion locks with each access. Therefore even in single-\/threaded programs, it\textquotesingle{}s more efficient to use local variables (even local variables with global scope -\/ where a local variable is declared in the top-\/level block) if the value of the variable does not need to be shared among other threads. {\bfseries{our Example}}~\newline \begin{DoxyCode}{0} \DoxyCodeLine{our int i = 1;} \end{DoxyCode} Note that the {\bfseries{our}} keyword is required when declaring a global variable if the parse option \mbox{\hyperlink{parse_directives_require-our}{\%require-\/our}} ({\ttfamily -\/O} or {\ttfamily --require-\/our} command-\/line option) is set for the parent program. See \mbox{\hyperlink{group__parse__options}{Parse Option Constants}} for more information. Unlike local variables, global variables are members of \mbox{\hyperlink{qore_namespaces}{namespaces}}; if a namespace path is not given, then the global variable will be assumed to be in the root namespace. Global variables declared in \mbox{\hyperlink{qore_namespaces}{namespaces}} cannot be initialized at the same time as the declaration, but instead have to be initialized elsewhere. When defining a \mbox{\hyperlink{qore_modules_user_modules}{user module}}, the {\bfseries{our}} keyword can be preceded by \mbox{\hyperlink{qore_modules_mod_public}{public}}, which means that the global variable will be available (imported) in the \mbox{\hyperlink{class_qore_1_1_program}{Program}} object importing the module. See \mbox{\hyperlink{qore_modules_mod_public}{public}} for more information.\hypertarget{variables_thread_local_variables}{}\doxysection{Thread-\/\+Local Global Variables}\label{variables_thread_local_variables} \hypertarget{variables_thread_local}{}\doxysubsection{The \char`\"{}thread\+\_\+local\char`\"{} Keyword}\label{variables_thread_local} Variables declared with {\bfseries{thread\+\_\+local}} have global scope and are additionally thread local; they are never subject to mutual exclusion locks. Each Qore thread will have a different value for the variable, for example. {\bfseries{thread\+\_\+local Example}}~\newline \begin{DoxyCode}{0} \DoxyCodeLine{thread\_local int i = 1;} \end{DoxyCode} Note that the {\ttfamily {\bfseries{thread\+\_\+local}}} keyword is always required when declaring a global thread-\/local variable. Like standard \mbox{\hyperlink{variables_global_variables}{global variables}} but unlike local variables, thread-\/local variables are members of \mbox{\hyperlink{qore_namespaces}{namespaces}}; if a namespace path is not given, then the thread-\/local variable will be assumed to be in the unnamed root namespace. Thread-\/local variables declared in \mbox{\hyperlink{qore_namespaces}{namespaces}} cannot be initialized at the same time as the declaration, but instead have to be initialized elsewhere. When defining a \mbox{\hyperlink{qore_modules_user_modules}{user module}}, the {\ttfamily {\bfseries{thread\+\_\+local}}} keyword can be preceded by \mbox{\hyperlink{qore_modules_mod_public}{public}}, which means that the {\ttfamily {\bfseries{thread\+\_\+local}}} variable will be available (imported) in \mbox{\hyperlink{class_qore_1_1_program}{Program}} objects importing the module. See \mbox{\hyperlink{qore_modules_mod_public}{public}} for more information. \begin{DoxyNote}{Note} \begin{DoxyItemize} \item {\ttfamily {\bfseries{thread\+\_\+local}}} global variables are always thread local, even if referenced in a \mbox{\hyperlink{expressions_closure}{closure}} or if an \mbox{\hyperlink{lvalue_references_references_overview}{lvalue reference}} is taken of a {\ttfamily {\bfseries{thread\+\_\+local}}} variable \item \mbox{\hyperlink{group__threading__functions_gae83c32576b59a1ecfa906d9b3c3af557}{Qore\+::set\+\_\+thread\+\_\+init()}} and \mbox{\hyperlink{class_qore_1_1_program_a6fda138e987e698d3c5b11294d21bcdb}{Program\+::set\+Thread\+Init()}} can be used to automatically assign values to \mbox{\hyperlink{variables_thread_local}{The \char`\"{}thread\+\_\+local\char`\"{} Keyword}} variables when threads are started \end{DoxyItemize} \end{DoxyNote}