FAQ

Yet another lexical variable proposal

J. David Blackstone
Aug 15, 2000 at 5:12 am
=head1 TITLE

Yet another lexical variable proposal: lexical variables made default
without requiring strict 'vars'

=head1 VERSION

Maintainer: J. David Blackstone <jdavidb@dfw.net>
Date: 14 Aug 2000
Version: 1
Mailing List: perl6-language-strict
Number:

=head1 ABSTRACT

Perl was originally designed around dynamically-scoped variables.
Many users would like to see this design flaw fixed, but are disagreed
about how to go about it. This proposal suggests making undeclared
variables be lexical by default in Perl6 and deals with the possible
ambiguities this could bring about. An optional suggestion is made as
to how one might go even further and eliminate dynamic variables
entirely from the language.

=head1 DESCRIPTION

Lexically-scoped variables are easy to use and intuitive, in that any
lexical variable refers to a variable declared within the current
scope or the enclosing scope. The variable can be located by
lexically scanning the source code. Dynamic variables, on the other
hand, refer to a variable from within the current scope or from within
the current subroutines _caller_, which could be anywhere! It is
impossible to tell exactly what else might be happening to a dynamic
variable, resulting in various action at a distance problems, variable
suicide problems, and other difficulties.

Under this proposal, lexical variables are considered to be the norm
for Perl6. Any undeclared variable is considered to be a lexical
variable.

=head2 Scopes

An undeclared variable is lexical and visible only within the scope
where it is first used and any scopes contained within that one. The
notion of "scope" is the same as Perl has had almost since the
beginning: a block (including a subroutine block) begins a new scope;
a file is also a scope.

Thus, in the following code segment,

$x = 15;
$y = $x;
while ($y) {
$z = $y;
...
$x += $z;
}

$x and $y are lexicals contained in the outermost scope (probably a
file), while $z is a lexical available only in the while loop. When
used within the while loop, $x and $y refer to the same scalars
referred to outside of the while loop.

=head2 Use of C<my>

In all cases, the C<my> operator behaves as it does in Perl5, allowing
local variables that will not interfere with other variables, etc.

=head2 Dynamic Assignment

Dynamic assignment is the technical term given to the action performed
by C<local> in Perl5 and earlier versions. The value of a variable is
saved upon execution of the operator and restored when the current
scope ends.

There is no actual reason why dynamic assignment needs to be limited
to dynamic variables. This RFC strongly suggests that dynamic
assignment be enabled for lexical variables, as well. Programming
with all lexicals and occasional use of dynamic assignment can cover
many of the cases where dynamically-scoped variables are useful.

Note that C<local> will probably be renamed in Perl6.

=head2 Ambiguity

Several people have raised issues about possible ambiguities with this
idea, but they have all been instances of the same problem: the case
where an undeclared variable is used first within a block, then within
that block's containing scope. For example,

$cond = ...;
if ($cond) {
...
$color = "blue";
...
}
print "The color is $color\n";

The programmer expects the value of $color to be "blue" for the print
statement, but in fact $color is a brand-new, undefined, lexical
variable.

Translating this block from Perl5 to get the same behavior in Perl6 if
this RFC is adopted is straightforward and discussed in the
IMPLEMENTATION section.

There are two options for dealing with this construct in new Perl6
code:

=over 4

=item 1

Dubbed the "conservative" approach by Mark-Jason Dominus, this option
requires that the programmer disambiguate the situation by declaring
the variable with C<my>. Perl would produce a warning in this case to
the effect that, "A variable used within a block was used after that
block, but never declared or used before it. The enclosing scope
cannot see the same variable that exists within the enclosed block."

Alternatively, if this RFC is adopted, but nothing is done to alert
new Perl6 programmers about these possibly ambiguous cases, the
programmer would receive a "Use of undefined value" warning which
might suffice.

=item 2

In the "liberal" approach, perl can do what amounts to "inferring
declarations." To actually refer to it this way would be a
contradiction in terms, since a declaration is explicit, not inferred.

To implement the liberal approach, perl would detect all of the
undeclared variables used within a scope when it compiles that scope.
These variables would become available for use from the minute that
scope is entered. Thus, in the example above, $color is detected as
being a part of the enclosing scope before the interpreter ever enters
the if statement, and $color therefore refers to the same scalar in
both places.

=back

=head2 Variable declarations

This proposal does not require variable declarations, like the strict
'vars' pragma does, except if the conservative approach is taken to
resolving the ambiguity noted above. Even then, declarations are only
required in a very few cases.

Some programmers will want a mechanism to require declarations,
similar to Perl5's strict 'vars' pragma. This is beyond the scope of
this RFC.

=head2 Possible elimination of dynamic variables

This subsection suggests a radical change for Perl6. Everything else
in this RFC can be implemented without implementing this idea, if
desired. This subsection should be considered "optional."

In most languages, dynamic scoping of variables offers no advantages
over lexical scoping, other than dynamic assignment. As noted above,
dynamic assignment can be accomplished with the C<local> operator,
which can be extended to operate on lexical variables as well as (or
even instead of) dynamic variables.

The chief instance where Perl5 requires dynamic variables is in the
case of package globals. The package command was created in order to
allow for different namespaces that would not conflict, when lexical
variables were not available at all in Perl. Now it has been extended
for O-O classes.

The following changes could be made involving lexical variables and
packages in order to eliminate dynamic variables from the language
entirely:

=over 4

=item *

Dynamic variables are removed from the language.

=item *

Lexical variables belong to the package they are declared in. (Or, if
not declared, the package to which they belong can be inferred from
the scope in which they are used.)

=item *

Lexical variables can only be accessed from within the same scope or a
lower scope. This means lexicals declared in the same file can be
accessed even if they belong to a different package, but lexicals
declared in another file cannot be accessed directly.

=item *

In nearly all cases the functionality of a package (dynamic) variable
that is intended for access from a different scope/file can be
provided by a lexical variable defined in a .pm file along with a
class accessor method (standard get/set functionality). This should
provide the ability to automate the translation of Perl5 code into
Perl6, should this RFC and this optional subsection be adopted.

=item *

Packages still continue to function in the same way to define classes.

=item *

Packages still continue to provide a separate namespace for non-O-O
subroutines.

=back

Under the proposal of this optional subsection, it might be desired to
implement a pragma to allow the use of dynamic variables.

=head1 IMPLEMENTATION

Very little will have to be done to translate Perl5 to Perl6 under
this proposal. The ambiguous case mentioned above where a variable is
used after a block, but not before it, can be disambiguated in all
cases with a declaration before the block (100% translation). This
works whether the "conservative" or "liberal" approach is adopted.

It is suggested that Perl6 be designed with lexicals in mind first,
followed by dynamic variables, if appropriate.

If the optional subsection on eliminating dynamic variables entirely
is adopted, Perl will completely shed the heritage of dynamic
variables. Whatever the case, the language should be designed and
implemented in such a way that lexical variables are in the design
with dynamic assignment/dynamic variables added in later, rather than
the other way around.

=head1 REFERENCES

There have been many alternative and conflicting proposals. This RFC
does not necessarily attempt to be consistent with any of them, but
they are listed here for convenience.

RFC 6: Lexical variables made default by means of strict 'vars'
RFC 16: Keep default Perl free of constraints such as warnings and
strict
RFC 64: New pragma 'scope' to change Perl's default scoping

=cut


------------------------------------------------------------------
Just because everything is negotiable doesn't mean that we plan to
negotiate everything away.
-- Larry Wall
reply

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post

1 user in discussion

J. David Blackstone: 1 post