Static and dynamic scope in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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