Skip Menu |
Report information
Id: 130958
Status: open
Priority: 0/
Queue: perl5

Owner: Nobody
Requestors: rjbs <rjbs [at] cpan.org>
Cc:
AdminCc:

Operating System: (no value)
PatchStatus: (no value)
Severity: low
Type: unknown
Perl Version: (no value)
Fixed In: (no value)



Subject: inconsistent block/hash detection (again)
From: Ricardo Signes <rjbs [...] cpan.org>
To: perlbug [...] perl.org
Date: Wed, 8 Mar 2017 14:12:32 -0500
Download (untitled) / with headers
text/plain 756b
I realize that the block/hash issue has had a lot of ink spilled over it, but this particular example surprised and baffled me (and Matthew Horsfall, who pointed it out to me today): ~$ perl -MData::Dumper -E 'say Dumper([ map {; { 1 => $_ } } qw(cat) ])' $VAR1 = [ { '1' => 'cat' } ]; ~$ perl -MData::Dumper -E 'say Dumper([ map {; { $_ => 1 } } qw(cat) ])' $VAR1 = [ 'cat', 1 ]; The inside {...} in these two are interpreted differently based on whether the first token is $_ or 1? On one hand, I wish these were consistent. On the other, I realize that this change might break some code somewhere. (Yes, I know you can force the issue with a unary plus.) -- rjbs
Download signature.asc
application/pgp-signature 473b

Message body not shown because it is not plain text.

To: perl5-porters [...] perl.org
Subject: Re: [perl #130958] inconsistent block/hash detection (again)
From: Dave Mitchell <davem [...] iabyn.com>
Date: Wed, 29 Mar 2017 14:02:57 +0100
Download (untitled) / with headers
text/plain 1.3k
On Wed, Mar 08, 2017 at 11:13:03AM -0800, Ricardo SIGNES wrote: Show quoted text
> # New Ticket Created by Ricardo SIGNES > # Please include the string: [perl #130958] > # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/Ticket/Display.html?id=130958 > > > > I realize that the block/hash issue has had a lot of ink spilled over it, but > this particular example surprised and baffled me (and Matthew Horsfall, who > pointed it out to me today): > > ~$ perl -MData::Dumper -E 'say Dumper([ map {; { 1 => $_ } } qw(cat) ])' > $VAR1 = [ > { > '1' => 'cat' > } > ]; > > ~$ perl -MData::Dumper -E 'say Dumper([ map {; { $_ => 1 } } qw(cat) ])' > $VAR1 = [ > 'cat', > 1 > ]; > > The inside {...} in these two are interpreted differently based on whether the > first token is $_ or 1? On one hand, I wish these were consistent. On the > other, I realize that this change might break some code somewhere.
I don't see that as being particularly surprising. The rule appears to be: if a literal constant follows the '{' it's probably a hash; if a variable follows the '{' its probably a block. -- Little fly, thy summer's play my thoughtless hand has terminated with extreme prejudice. (with apologies to William Blake)


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org