[% setvar title Lexical variables made default %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Lexical variables made default
Maintainer: J. David Blackstone <jdavidb@dfw.net> Date: 1 Aug 2000 Last Modified: 26 Sep 2000 Mailing List: perl6-language-strict@perl.org Number: 6 Version: 3 Status: Frozen
Prior to version 5, all implementations of Perl were designed with only dynamic (global) variables in mind. Perl5 provided lexical variables in a backward compatible way, along with a pragma to force either declaration of variables or import of dynamic variables. Perl6 should make lexical variables and required variable declarations the default.
The author cannot decide whether doing this with something analogous
to a use strict 'vars'
is better or worse than doing this with a
proposal like RFC 106. Although the RFC 106 proposal seemed to gather
more interest, there were still enough naysayers that I believe it
would be incorrect to claim a consensus (as to which, if either,
proposal should be implemented).
These two RFCs should be read in comparison with each other.
Dynamically-scoped variables can have many effects which are
counterintuitive and undesirable. In Perl5, lexically-scoped
variables are available with the my
operator. Without my
, a
variable is assumed to be dynamic.
Perl5 provides the strict
pragma, which forces the programmer to do
one of three things: 1) declare a variable to be lexical with my
;
2) declare a variable to be dynamic with our
; 3) import a dynamic
variable from another namespace with vars
or otherwise. This
forced declaration of variables is generally desirable as good
programming practice, although the ability does exist to turn off the
pragma when necessary.
There are very few circumstances in which dynamic variables have a
distinct advantage over lexicals. Most modules in the standard
library are deliberately written with strict 'vars'
to avoid
interfering with other modules or the application programmer's code.
More recent and enlightened documentation and educational material
teaches the use of lexical variables as preferred.
In Perl6, the concept of strict 'vars'
should on by default. Every
variable should be declared either as lexical with my
or dynamic
with our
. New Perl6 programmers should be immediately introduced
to lexical scoping and its benefits. The ability to disable the
strictness should be retained through no strict 'vars';
or an
equivalent, for "quick-and-dirty" programs, backward compatibility,
and where otherwise necessary or desired by those who Know What They
Are Doing.
Attempts to silently make all undeclared variables lexical does not address the benefits to be gained by forcing declaration of all variables. Declarations provide quite a bit of information to the compiler, including a notion of exactly which block a variable should be scoped in. Declarations are also now being used in various proposals involving strong(er?) typing, constants, O-O, and so on.
This RFC is not suggesting that any of the other features of the
strict
pragma be on by default (such as 'refs'
or 'subs'
).
If that is desired, it should be submitted in a separate RFC.
The local
operator is misnamed, since it operates on dynamic
(global) variables. This operator should be removed or renamed, but
this should be the subject of a separate RFC.
I don't know enough about internals to say anything about implementation from that standpoint.
The following program examples show how declarations would work.
my $name = "J. David"; # lexical variable our($VERSION) = 5.31; # dynamic variable in current package $this *= 7; # illegal; variables must be # declared lexical or dynamic my($arg1, $arg2) = @_; # lexical (traditional for a subroutine) { no strict 'vars'; # or something new $quick = 7; # legal in this scope = # dynamic variable in current package $quick *= $main::var; print "$quick\n"; } $package::count++; # always legal (I presume?)
Although the implementation above is currently considered to be the
best, an alternative would allow undeclared lexical variables. In
this approach, strict 'vars'
would become strict 'decs'
,
requiring declaration of all variables. Without strict 'decs'
,
undeclared variables would be considered to be lexical, or variables
could be declared dynamic with our
or some other syntax.
RFC 106 gives more details on such an alternative implementation.
This approach would require more complexity in Perl5 -> Perl6
translation and be a much larger step in a new direction for the
language. Since the state-of-the-practice of Perl5 programming has
evolved to generally include strict 'vars'
, it makes more sense to
simply make that the default than to modify the language any further.
RFC 106: Yet another lexical variable proposal: lexical variables made default without requiring strict 'vars'
The section on "Private Variables via my()" in the perlsub manpage.
The Camel book.