[% setvar title Alternate Syntax for variable names %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


Alternate Syntax for variable names


  Maintainer: David Corbin <dcorbin@machturtle.com>
  Date: 20 Aug 2000
  Last Modified: 28 Aug 2000
  Mailing List: perl6-language@perl.org
  Number: 133
  Version: 2
  Status: Developing


Many new users are confused by the use of $@% to represent context, when it is also used to declare variables. This is a syntactic change that introduces a bit more logic to the context/type confusion.


Context is an essential part of Perl. When evaluating a symbolic expression, $, @ and % are used to indicate the context of the expression. However, when variables are declared (using local, my, or simply implicitly as an lvalue) these same symbols are used. my @array; my %hash; $var=1 To many people, most notably programmers new to Perl, the $@% is mistakenly believed to be part of the variable name. This leads to such erroneous attempts to use them as @array[0], and @%hash{key}.

Consider the following syntax:

 my var; 	# declaring a scalar
 my array[]; # declaring an array
 my hash{};	# declaring a hash

Then, when it is necessary to distinguish context explicitly (it often is not), you can use $@% as before. Consider:

 count = array;		 # scalar context because of assignment to scalar.
 alt_array[] = array; # list context 

 value = hash{key};   #

 print $array," ",@array #Context must be clearly designated.

I'm not the linguist that Mr. Wall is, but it strikes me that context should be derrived automatically as much as possible.

An slightly different alternative would be that arrays and hashes are always referred to with their trailing indicator ([] or {}). So, from the example above, you'd have

 alt_array[] = array[];




RFC 9: Highlander Variable Types

RFC 109: Less linenoise - let's get rid of @%