Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Lexical scoping like a fox

by ig (Vicar)
on Aug 28, 2009 at 00:00 UTC ( #791779=note: print w/ replies, xml ) Need Help??


in reply to Re: Lexical scoping like a fox
in thread Lexical scoping like a fox

I'll get even pickier... but likewise meant constructively.

Regarding the scope of a lexical variable, it actually begins after the statement in which the variable is declared to be lexical. Here are two examples that demonstrate this.

my $r = "set in file scope"; { print "at beginning of BLOCK value of \$r is: $r\n"; print "in conditional statement value of \$r is: $r\n" if my $r = "set in if condition"; print "at end of BLOCK value of \$r is: $r\n"; } print "after BLOCK value of \$r is: $r\n";

produces

at beginning of BLOCK value of $r is: set in file scope in conditional statement value of $r is: set in file scope at end of BLOCK value of $r is: set in if condition after BLOCK value of $r is: set in file scope

and

my $r = "set in file scope"; { print "at beginning of BLOCK value of \$r is: $r\n"; my $r = "set in BLOCK's scope but formerly: $r"; print "at end of BLOCK value of \$r is: $r\n"; } print "after BLOCK value of \$r is: $r\n";

produces

at beginning of BLOCK value of $r is: set in file scope at end of BLOCK value of $r is: set in BLOCK's scope but formerly: set + in file scope after BLOCK value of $r is: set in file scope

Also, I think equating "label" with "variable" might be confusing.

In this context I would say that a variable is composed of a label and a value. It is neither one nor the other - it is the combination. Having said this, I would then use label where you have used variable, or perhaps symbol (as in symbol table) or identifier. But for consistency here, I will use label.

To continue with your analogy... What is in the luggage is variable and which piece of luggage a label is tied to is also variable. The local function/declaration puts aside the original piece of luggage, with its contents undisturbed, and ties the label to a new, empty piece of luggage. You can put anything you like into this new piece of luggage. When execution of the program leaves the scope in which local was used, the label is removed from the new piece of luggage and tied, once again, to the original piece of luggage. The new piece of luggage may then be destroyed, if it has no other labels attached to it. Note also that the original piece of luggage may have had other labels attached to it before local was used, and these other labels can still be used to access the original luggage, even while the first label is attached to the new luggage.

This last point is demonstrated by the following:

my $r = "set in file scope"; *original = \$r; { my $r = "set in BLOCK scope"; print "\$r is: $r\n"; print "\$original is: $original\n"; }

which produces

$r is: set in BLOCK scope $original is: set in file scope

Digging a little deeper... There is even more potential for confusion when one realizes that values are stored in a set of nested data structures. Labels (entries in symbol tables and pads) are associated with globs which are associated with scalars, arrays, hashes, etc. and these scalars, arrays, hashes, etc. are associated with values (I say associated as a euphemism for refer to, point at, have or contain, as the case may be). Among all these parts, which is/are the variable? Which is/are the value? Which parts change when local is used? What changes when the variable is assigned to? This is too much detail for this tutorial, yet the terminology used in the tutorial should be consistent with and facilitate an easy transition to a more detailed understanding and discussion, as much as possible (after all, simplification and abstraction aren't if they perpetuate all the detail).

The distinctions become important when one deals with references, and even more so if the variables (or is it the values) have magic. Consider the following

$x = 'x'; @x = qw(x x x); $y = 'y'; @y = qw(y y y); $z = 'z'; @z = qw(z z z); print "$x,@x : $y,@y : $z,@z\n"; *y = *x; print "$x,@x : $y,@y : $z,@z\n"; *z = \$x; print "$x,@x : $y,@y : $z,@z\n"; { local $x = 'lx'; local @x = qw(lx lx lx); print "$x,@x : $y,@y : $z,@z\n"; }

which produces

x,x x x : y,y y y : z,z z z x,x x x : x,x x x : z,z z z x,x x x : x,x x x : x,z z z lx,lx lx lx : lx,lx lx lx : x,z z z

In a simple model, neither the labels nor the values are variable. What is variable is the relationship between them. A label may be associated first with one and then later with another value. A value may have many labels associated with it. Thus, again, I would say the variable (noun) is the combination, not one part or the other. To refer to the parts, it is better to use the terms label (or perhaps symbol or identifier) and value. And, as noted earlier, values may themselves be complex and require further terminology to identify their parts and relationships.

As these terms are used so variably throughout the documentation, it is best to be explicit where a specific meaning is intended.


Comment on Re^2: Lexical scoping like a fox
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://791779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2014-09-22 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (200 votes), past polls