Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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";


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


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";


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.

In reply to Re^2: Lexical scoping like a fox by ig
in thread Lexical scoping like a fox by broquaint

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-09-16 16:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found