develooper Front page | perl.perl5.porters | Postings from February 2016

Re: [perl #127552] No warning or error on @$

Thread Previous
February 16, 2016 16:24
Re: [perl #127552] No warning or error on @$
Message ID:
On Tue, Feb 16, 2016 at 07:40:33AM -0800, Ed Avis via RT wrote:
> 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 find it surprising you expect pervar to enumerate all possible
variable names.

> 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.

If "makes typos hard to spot" is a reason to warn, should perl also warn
if one uses two variables with similar names? $l vs $1, for instance.
Or $( and $).

I think scanning for variable names which can be easily mistaken for
a typo would be an excellent rule for perlcritic -- the user can then
tweak it to her preference depending what she considers an easy typo,
or a sensible name.

> 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.

Perhaps because it doesn't have any semantics? ;-)

But *do* note that the *$ slot *does* have a variable with special
meaning: $$. 

> 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.

That would lead to forbidding @$, but allowing $$. 

> 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.

Other than growing the size of the manual page, what does listing all
unused combinations of $?, @?, %? gives us? Wouldn't a line like

"any variable not listed here does (currently) not have a special meaning"

work as well?


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About