Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created November 12, 2010 22:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DmitrySoshnikov/674782 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/674782 to your computer and use it in GitHub Desktop.
Static and dynamic scope in Perl
# Lexical and dynamic scopes in Perl;
# Static (lexical) scope uses model of
# environments with frames; dynamic scope
# uses single global var frame.
$a = 0;
sub foo {
return $a;
}
sub staticScope {
my $a = 1; # lexical (static)
return foo();
}
print staticScope(); # 0 (from the saved global frame)
$b = 0;
sub bar {
return $b;
}
sub dynamicScope {
local $b = 1;
return bar();
}
print dynamicScope(); # 1 (from the caller's frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment