No warning or error on @$ #15184
Created by @epa
Perl silently accepts an array variable @$
% perl -E 'use warnings; use strict; say @$ '
This is unfortunate since it can be a typo for $@.
It might be worth going through all the other ASCII punctuation
my $man = `man perlvar`;
I suggest any special punctuation variable not documented should cause an error.
Ed Avis wrote:
$ perl -lwe '@$ = qw(a b c); print @$'
The array variable works. Making it an error would be an incompatible
I think a deprecation warning would be fine, as long as there is a warning.
OTOH, if it is felt that oddball array variables like @$ work and should
On Tue, Feb 16, 2016 at 02:22:26PM +0000, Ed Avis wrote:
perlvar already states:
Perl identifiers that begin with digits, control characters, or
Did you miss that, or do you feel that more is required?
I understand that perl special variables like $! are accessible from all packages and do not need to be declared first. That is not the issue here. The question is which special variables exist in the first place.
I was surprised to find that a special variable @$ exists, and produces no error or warning on use, despite not being mentioned in perlvar. That manual page does say that all variable names consisting of a sequence of digits, or a single punctuation character, are reserved for special uses. It then lists the special uses of punctuation variables which currently exist. I had expected that if a punctuation variable was not mentioned in that list, then it would not exist and attempting to use it would be an error. Instead, the current implementation is that all possible single-character variables do exist, and those not mentioned in perlvar have the normal semantics of arrays or hashes. (For scalars, all single-character variables are mentioned.)
I think that if this is considered the documented behaviour then it is less than ideal because firstly, it makes typos such as @$ for $@ hard to spot, and secondly it makes it impossible to add new special single-character array or hash variables in future.
But actually, in my opinion perlvar doesn't make it clear one way or the other. It says the variable names are reserved (which normally indicates that you shouldn't use it for ordinary uses) but when one is not mentioned in the list of special variables, doesn't say what the semantics of it are.
So there are two options here:
Either - Disallow the use of single-character punctuation variables which aren't documented. Since there may be current code using them, it should be done with a deprecation cycle.
Or - Document in perlvar the remaining single-character punctuation variables not currently mentioned, saying that they have the ordinary array and hash semantics (but of course the names are accessible in all packages and do not need declaring). I think this would be an unhappy choice for the reasons given above. But at least it would clarify things.
On Tue, Feb 16, 2016 at 07:40:33AM -0800, Ed Avis via RT wrote:
I find it surprising you expect pervar to enumerate all possible
If "makes typos hard to spot" is a reason to warn, should perl also warn
I think scanning for variable names which can be easily mistaken for
Perhaps because it doesn't have any semantics? ;-)
But *do* note that the *$ slot *does* have a variable with special
That would lead to forbidding @$, but allowing $$.
Other than growing the size of the manual page, what does listing all
"any variable not listed here does (currently) not have a special meaning"
work as well?
Abigail <abigail <at> abigail.be> writes:
Indeed. $$ is a special variable documented in perlvar. @$ is not,
I think you misunderstand - documenting them does not have to mean a
However, giving a documented semantics to the currently unused single-
Currently, since @/ is unused and perlvar (in my opinion) doesn't specify
Therefore, what I suggest is to document that single-character punctuation
It will also help to spot typos - I see that you do not see much value in
From the keyboard of Ed Avis [16.02.16,16:38]:
Their semantics are those of ordinary variables and denoted by the
This is exactly what is stated in perlvar:
Perl variable names may also be a sequence of digits or a single
Which means (but could be stated explicitly) that those variables may be
I have never seen use of such variables in devent production code or
It would be sad if gems like e.g. Erudil's CamelCode would become just
Any punctuation variable may be used for any special purpose, and if it
I am with Abigail on that: excellent candidates for perl critic.
Apr 5, 2016
On Tue, Feb 16, 2016 at 08:45:31PM +0100, shmem wrote:
Conversely, one important use of 'use strict' is to spot typos in
I think I'm in favour of emitting a warning each time a slot of a
'@$' is a reserved punctuation variable
while of course '$$' wouldn't.
This wouldn't affect golf, japh's etc unless they are run with -w
perlvar would make it clear that any undocumented vars will emit this
Migrated from rt.perl.org#127552 (status was 'open')
Searchable as RT127552$
The text was updated successfully, but these errors were encountered: