Thursday, May 12, 2011

Dynamic and Lexical Scope in Perl

1 What is Scope?

1.1 The dictionary definition of "scope:"

  • The range of one's perceptions, thoughts, or actions.
  • The Breadth or opportunity to function.

1.2 As applied to variables in computer languages:

  • Scope refers to the visibility of variables within a program; for example, whether one function can see a variable created in another function.
  • Scope is a context within which to resolve names.

2 Why We Care About Scope

  • We want to know where a variable be seen in our program.

2.1 Two Questions

  1. If I declare a variable in a subroutine, will other subroutines be able to access it?
    • I \textcolor{red}{don't} want them to be able to access my variables!
    • I \textcolor{red}{do} want them to be able to access my variables!
  2. If another subroutine creates a variable, can I use it in my subroutine?

2.2 Bottom Line

  • Different kinds of scope used in different situations may make our programs easier to write and debug.

3 Two Types of Scope

  • Today, we'll be investigating two types of scope that are defined in Perl (and many other languages).
    1. Dynamic
    2. Lexical

4 Dynamic Scope — What is it?

  • Dynamic scope is a property of program execution.
  • Dynamic scope is global:
    • That is, variables that have dynamic scope can be seen everywhere.
  • Dynamic scope allows shadowing
    • Multiple instantiations of a variable hide previous instantiations.

5 Dynamic Scope Implementation Overview

  • Each variable has a global stack of bindings
  • Introducing a variable, for example, 'x' pushes a binding on x's global stack.
  • Evaluating x in any context yields the top of x's stack.
  • When program flow leaves x's scope, the binding is popped off x's global stack.

6 Dynamic Scope: Pseudo-code Example:

6.1 C-Like Pseudo-code

int x = 0;
int f() { return x; }
int g() { int x = 1; return f(); }
g();
print x;

6.2 Note

  • When function f() is called from g(), what is the value of 'x' inside of f()?
    • The value of x inside of function f is 1.
  • What is the value of x in the print statement?
    • In the print statement the value of x is 0.
  • With dynamic variables knowing the flow of control of the program is essential.

7 Dynamic Scope Summarized

  • Dynamic variables are global
  • New instantiations shadow previous instantiations
  • Dynamic variables depend on the path of program execution.

8 Lexical Scope — What is it?

  • Lexical scope is a property of the program text.
  • With lexical scoping, the name of a variable always refers to the \textcolor{red}{local lexical environment}.
  • Thus, when a lexical environment is created, only variables created in that lexical scope are visible.
  • A lexically scoped variable is private to the block in which it is declared and is available only to that block.
    • It is not available to subroutines that are called from the block.
  • Sub-lexical scopes can, by definition, see variables in the outer lexical scope.

9 Lexical Scope: Pseudo-code Example:

9.1 C-Like Pseudo-code

int x = 0;
int f() { return x; }
int g() { int x = 1; return f(); }
g();

9.2 Note

  • When function f() is called from g(), what is the value of 'x' inside of f()?
    • It would be 0.
  • Assuming lexical scoping, why can the function f see any x at all?
  • Notice that with lexical variables, the code becomes immediately more modular.

10 Dynamic Scope in Perl (1)

10.1 Perl Code:

$x = 0; 
sub f() { 
  print "From function f:\$x=$x\n";
}
sub g() {
  $x = 1; 
  f();
}
g();
print "Finished:\$x=$x\n";

10.1.1 Output:

From function f:$x=1
Finished:$x=1

10.1.2 Note:

  • "$x = 0;" at the top of the program is unnecessary.

11 Dynamic Scoping in Perl (2)

11.1 Perl Code:

sub f() { 
  print "From function f:\$x=$x\n";
}
sub g() {
  local $x = 1; 
  f();
}
g();
print "Finished:\$x=$x\n";
if (not defined $x) {
  print "x is not defined\n";
}

11.2 Output:

From function f:$x=1
Finished:$x=
x is not defined

11.3 Note:

  • $x is instantiated as local in function g and therefore available in function f.
  • At the end of the program, x is not defined.

12 Dynamic Scoping in Perl (3)

12.1 Perl Code:

$x = 0; 
sub f() {
  print "From function f:\$x=$x\n";
}
sub g(){
  local $x = 1; 
  f();
}
g();
print "Finished:\$x=$x\n";

12.2 Output:

From function f:$x=1
Finished:$x=0

12.3 Note:

  • We see in this case that using local caused the initial value of the global $x to be shadowed.

13 Summary of Dynamic Scope in Perl

  • Dynamic scope is global
  • Dynamic scope allows shadowing
  • Dynamic scope in perl is created using the keyword local.

14 Lexical Scoping in Perl

14.1 Perl Code:

$x = 0; 
sub f() {
  print "From function f:\$x=$x\n";
}
sub g() {
  my $x = 1; 
  f();
}
g();
print "Finished:\$x=$x\n";

14.2 Output:

From function f:$x=0
Finished:$x=0

14.3 Note:

  • Lexical scope is created with my
  • The lexical variable x in function g is not available in function f.

15 Global Lexicals

15.1 Perl Code:

my $x = 0;
sub f() {
  print "From function f:\$x=$x\n";
}
my $x = 1;
sub g() { 
  print "From function g:\$x=$x\n";
}
f();
g();

15.2 Output:

From function f:$x=0
From function g:$x=1

16 Lexical Scope in Perl Summary

  • Lexical scope makes a variable private so that it can only be seen in its lexical block.
  • Lexical scope in perl is created using the keyword my.

Date: 2011-05-12 09:45:34

Author: Karl Stump

Org version 7.5 with Emacs version 23

Validate XHTML 1.0