Unformatted text preview:

Functions and ScopeNames and BindingsMany MoonsSeparate Places for EachThe IdeaLexical ScopeLexical Scope (2)Enclosing ScopesHow Lexical Scopes WorkLexical Scopes ContinuedLexical Scopes Continued The Meaning of "Lexical"Perl's Pre-Defined VariablesThe local() Declarationlocal() Declarations local() DeclarationsExampleWarning User-Defined Global VariablesThe vars PragmaWarning AgainFunction DeclarationsExample with Function DeclarationRecursionRecursive FunctionsRecursive Perl FunctionsRecursion is Not EfficientWhy Use Recursion?Rules For Creating Recursive FunctionsExample 1Example 2Slide 32Copyright 2006 Stewart WeissCopyright 2010 Stewart WeissThe Scoop on ScopeThe Scoop on ScopeBinding names to things in Perl2 CSci 132 Practical UNIX with PerlNames and bindingsWhen you declare a variable in a program, as inmy $count;you are telling Perl to associate the name $count to a storage cell in memory. Henceforth, the name $count is bound to the location where its data is stored. You cannot writemy $count;my $count;because that would create two identical names for different locations and Perl would not know which was which. Must names be unique in programs?3 CSci 132 Practical UNIX with PerlMany moonsIn this program, there are two variables named $user. sub DisplayGreeting{ my $user = $_[0]; print "$user, Welcome to the program.\n";}print "Enter your name:";chomp( my $user = <STDIN> );DisplayGreeting($name); There can be many moons ...4 CSci 132 Practical UNIX with PerlSeparate places for eachThe first occurrence of $user is inside the block of the DisplayGreeting function. The second is outside of the block, in the main program:sub DisplayGreeting{ my $user = $_[0]; print "$user, Welcome to the program.\n";}print "Enter your name:";chomp( my $user = <STDIN> );DisplayGreeting($name);5 CSci 132 Practical UNIX with PerlThe idea The same name can occur in different blocks of a program because of the concept of scope.Scopes are like enclosing walls around sets of names. We can declare the same name within different scopes because as soon as execution leaves the confines of one scope, it no longer "knows" the names from that scope.We can have scopes inside of scopes, like Russian matryoshka dolls, each inside the next. Each scope can have a variable named $here.6 CSci 132 Practical UNIX with PerlLexical scopeRecall from Lesson 14 that a my() declaration creates a variable that can be used from that point forward in the innermost enclosing block.The lexical scope of a variable is the portion of the program in which that variable is accessible.This implies that the lexical scope of a variable declared with a my() declaration is the portion of the program from the declaration until the end of the block in which the declaration was made.7 CSci 132 Practical UNIX with PerlLexical scopeIn the block below, the lexical scope of $user extends from its declaration until the curly brace ending the block.{ my $user = $_[0]; # scope starts here print "$user, Welcome to the program.\n";} # end of block containing declaration# $user is out-of-scope here# If we referred to $user here, it would not be the same variable.8 CSci 132 Practical UNIX with PerlEnclosing scopesIn the code below, the scope of the first $x is blue; the second, pink, and the innermost, gold. Note that $z is visible within the innermost block.my ($x, $y, $z) = (1,2,3); { }print $x, $y; #prints 1 2 my $x = 4; { my $y = 5; $x = $x + $z; # $x = 4 + 3 } print $x, $y; # prints 7 2my $y = 6; print $x,$y ; # prints 3 6}9 CSci 132 Practical UNIX with PerlHow lexical scopes workPerl maintains a symbol table for each block. The symbol table contains the names of all of the lexical variables declared within that block.The file containing the program is also a block, so Perl has a symbol table of the lexical variables declared at "file scope" too. The file's block is the outermost block.At any given time during program execution, there is a current block. This is the innermost block containing the statement being executed. If execution enters a block contained within the current block, it becomes the new current block.10 CSci 132 Practical UNIX with PerlLexical scopes continuedEach time a new block is entered, Perl pushes the symbol table for the new block on top of the table for the old block, like pushing a cafeteria tray on top of the stack of used trays.When a block is exited, Perl "pops" the current symbol table off of the stack of blocks that it has pushed down.Suppose Block A contains Block B and Block B contains Block C, and execution passed from A to B to C. Then C is "on top of" B, which is "on top of" A. When a lexical variable is used in a statement, Perl looks at the symbol table for the current block.11 CSci 132 Practical UNIX with PerlLexical scopes continuedWhen a lexical variable is used in a statement, Perl looks at the table for the current block. If it finds the variable declared in that table, it uses that variable's value. If not, it looks at the table of the next outermost block, which is "under" the current one in the stack. if it finds it there, it uses its value, otherwise it looks outward again, which means "under" that block again.It continues to do this until either it reaches a block whose table has the declaration, or it reaches the outermost scope and does not find it. In this case, it is a new, undeclared variable without a value, so its value is undef.12 CSci 132 Practical UNIX with PerlThe meaning of "lexical"The scope created by a my() declaration is called a lexical scope because which declaration a variable refers to is purely determined by the program's text. The word "lexical" means textual. Because lexical scope rules are not determined by any runtime behavior, lexical scope is also called static scope.13 CSci 132 Practical UNIX with PerlGlobal scopeThe pre-defined variables, such as $_ and @ARGV, are included in the main program's symbol table automatically. All variables declared in that table are called global variables, because they are visible in all parts of a program.Variables declared using my() in file scope are not global; they are lexical variables with file scope.You cannot create lexical versions of Perl's pre-defined variables. In other words, you cannot write my $_; anywhere in your program.14 CSci 132 Practical UNIX with PerlThe local() declarationThe local() declaration is used to hide the values of global variables temporarily.


View Full Document

CUNY CSCI 132 - The Scoop on Scope

Download The Scoop on Scope
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Scoop on Scope and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Scoop on Scope 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?