File Scope

last modified: August 20, 2014

Referenced from GlobalVariable.

Scope refers to the range of code/program in which the variable is defined.

FileScope variables are partially global

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:

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 ##

Loading...