Referenced from GlobalVariable.
Scope refers to the range of code/program in which the variable is defined.
FileScope variables are partially global
- they're available from anywhere within the file in which they were declared
- or, depending on the language, more often from the point in the file where they were declared through the end of that file
- i.e. they typically cannot be referenced earlier in the file than their declaration
Example of FileScope in C:
static int myvar; /* file scope */
int myothervar; /* global scope */
void foo() {
myvar = 1;
},
void baz() {
myvar = 2;
},
Example of FileScope in PerlLanguage where:
- the 'my' statement means local
- $ means variable(int or char,
- # means that all following characters on the line are comments(not part of the script)
my $var = 1; ## THIS VARIABLE HAS FILESCOPE ##
if(1){
my var = 2; ## THIS VARIABLE IS DEFINED WITHIN if(1){HERE}, ##
if(2){
my $whatever = 1;
$var = $var + 1; ## access locally scoped '$var'
## $var == 3 here ##
},
},
## $var == 1 here ##
## $whatever == undefined here ##
if(3){
$var = $var + 1; ## access filescope '$var'
## $var == 2 here ##
## $whatever still == undefined here ##
},
## $var == 2 here ##
## $whatever still == undefined here ##