RFC 83 (v1) Make constants look like variables

[email protected] (Perl6 RFC Librarian)
Newsgroups perl.perl6.language,perl.perl6.announce
Message-ID <[email protected]>
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Make constants look like variables

=head1 VERSION

  Maintainer: Jeremy Howard <[email protected]>
  Date: 10 August 2000
  Version: 1
  Mailing List: [email protected]
  Number: 83

=head1 ABSTRACT

This RFC proposes that the current constant.pm module removed, and
replaced with a syntax allowing any variable to be marked as constant.

=head1 DESCRIPTION

Currently, constants are created in Perl using the constant.pm module:

  use constant PI => 3.1415926;

which creates an inlined subroutine:

  sub PI () {3.1415926;}

This method of creating constants has three serious drawbacks:
 
=over 8

=item Can not be interpolated in strings

Whereas variables can be interpolated into strings (e.g. "PI is $Pi"),
subroutines can not be. This makes using constants inconvenient, since
string concatenation must be used.

=item Inconsistant syntax

The sudden appearance of barewords can be quite unsettling to new users.
After becoming told that 'arrays are @name, scalars are $name, ...', the
rule suddenly stops working just because the programmer wants the value to
stay constant.

=item Redundant warnings

In persistant Perl environments such as mod_perl, inlined subroutines
often created the redundant warning 'Constant subroutine PI redefined'.
This has been a frequent source of confusion amongst new mod_perl users.

=back

It is proposed that a new syntax for declaring constants be introduced:

  my constant $PI = 3.1415926;
  my constant @FIB = (1,1,2,3,5,8,13,21);
  my constant %ENG_ERRORS = (E_UNDEF=>'undefined', E_FAILED=>'failed');
  
Constants can be lexically or globally scoped (or any other new scoping
level yet to be defined).

=head1 IMPLEMENTATION

Constants should have the same behaviour as the do now. They should be
inlined, and constant expressions should be calculated at compile time.

=head1 REFERENCES

perldoc constant; perldoc perlsub (for constant subroutines)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.