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

[perl #133827] Bug: An array or hash variable can take an integer asits name.

Thread Next
From:
Tony Cook via RT
Date:
February 5, 2019 22:38
Subject:
[perl #133827] Bug: An array or hash variable can take an integer asits name.
Message ID:
rt-4.0.24-19613-1549406320-1904.133827-15-0@perl.org
On Tue, 05 Feb 2019 12:06:23 -0800, twwilliams@k12.wv.us wrote:
> On Perl 5.26 on Windows, an array or hash variable can take an integer
> as its name.
> 
>     C:> perl -e "@3=(10,20,30); print $3[1]"
>     20
>     C:>
> 
> Multiple digits work too.
> 
>     C:>perl -e "@217=('aa','bb','cc','dd'); print $217[2]"
>     cc
>     C:>
> 
> ...and hashes.
> 
>     C:>perl -e "%217=('aa','bb','cc','dd'); print $217{aa}"
>     bb
>     C:>

This isn't a bug.

> ...but not scalars.
> 
>     C:>perl -e "$7='seven'; print $7"
>     Modification of a read-only value attempted at -e line 1.

Neither is this.  Numeric named scalars are reserved for regular expression captures:

C:\Users\Tony>perl -le "$_ = 'abc123'; /(\d+)/; print $1"
123

From perlvar:

 Perl variable names may also be a sequence of digits, a single
 punctuation character, or the two-character sequence: "^" (caret or
 CIRCUMFLEX ACCENT) followed by any one of the characters "[][A-Z^_?\]".
 These names are all reserved for special uses by Perl; for example, the
 all-digits names are used to hold data captured by backreferences after
 a regular expression match.

Tony

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=133827

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About