Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash Behaviour differs v5.6.0 to v5.6.1 #4523

Closed
p5pRT opened this issue Oct 24, 2001 · 11 comments
Closed

Hash Behaviour differs v5.6.0 to v5.6.1 #4523

p5pRT opened this issue Oct 24, 2001 · 11 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 24, 2001

Migrated from rt.perl.org#7840 (status was 'resolved')

Searchable as RT7840$

@p5pRT
Copy link
Author

p5pRT commented Oct 24, 2001

From paul.barry@itcarlow.ie

Here's some code​:

#! /usr/bin/perl -w

use strict;
use 5.6.0;

use constant HASH_KEY1 => 100;
use constant HASH_KEY2 => 200;

our %hash_desc = (
  HASH_KEY1 => "This is hash key one",
  HASH_KEY2 => "This is hash key two"
);

foreach my $h_desc ( sort keys %hash_desc )
{
  print "$h_desc -> $hash_desc{$h_desc}\n";
}

When executed under 5.6.0 the output is​:

100 -> This is the hash key one
200 -> This is the hash key two

When executed under 5.6.1 the output is​:

HASH_KEY1 -> This is the hash key one
HASH_KEY2 -> This is the hash key two

What gives? Why won't hashes in 5.6.1 use constants properly? (Please
don't tell me that Perl 5.6.0 is wrong ... I just might cry.)

I'm running on LinuxPPC 2000, with Perl built from downloaded sources
(no special options - "sh Configure -de").

Thanks.

--
Paul Barry, Lecturer (CPM), Institute ot Technology, Carlow, Ireland
E​: paul.barry@​itcarlow.ie W​: http​://glasnost.itcarlow.ie/~barryp
Tel​: +353-503-70400 Fax​: +353-503-70500

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2005

From @schwern

[paul.barry@​itcarlow.ie - Tue Oct 23 22​:13​:02 2001]​:

#! /usr/bin/perl -w

use strict;
use 5.6.0;

use constant HASH_KEY1 => 100;
use constant HASH_KEY2 => 200;

our %hash_desc = (
HASH_KEY1 => "This is hash key one",
HASH_KEY2 => "This is hash key two"
);

foreach my $h_desc ( sort keys %hash_desc )
{
print "$h_desc -> $hash_desc{$h_desc}\n";
}

When executed under 5.6.0 the output is​:

100 -> This is the hash key one
200 -> This is the hash key two

When executed under 5.6.1 the output is​:

HASH_KEY1 -> This is the hash key one
HASH_KEY2 -> This is the hash key two

What gives? Why won't hashes in 5.6.1 use constants properly? (Please
don't tell me that Perl 5.6.0 is wrong ... I just might cry.)

5.6.0 was wrong, sorry. A lot of things about 5.6.0 were wrong.

$ perl5.4.5 -wle 'use constant FOO => 100; %h = ( FOO => 42 ); print
$h{"FOO"}'
42
$ perl5.5.4 -wle 'use constant FOO => 100; %h = ( FOO => 42 ); print
$h{"FOO"}'
42
$ perl5.6.2 -wle 'use constant FOO => 100; %h = ( FOO => 42 ); print
$h{"FOO"}'
42
$ perl5.8.6 -wle 'use constant FOO => 100; %h = ( FOO => 42 ); print
$h{"FOO"}'
42

=> is documented as follows​:

The => digraph is mostly just a synonym for the comma operator. It's
useful for documenting arguments that come in pairs. As of release
5.001, it also forces any word to the left of it to be interpreted as a
string.

*Any* word, including one that might also be a constant, is interpreted
as a string. %h = ( CONSTANT() => 42 ); is a work around.

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2005

@schwern - Status changed from 'open' to 'resolved'

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2005

From @schwern

[schwern - Thu Jul 14 16​:47​:07 2005]​:

=> is documented as follows​:

The => digraph is mostly just a synonym for the comma operator. It's
useful for documenting arguments that come in pairs. As of release
5.001, it also forces any word to the left of it to be interpreted as a
string.

*Any* word, including one that might also be a constant, is interpreted
as a string. %h = ( CONSTANT() => 42 ); is a work around.

Reopening this bug.

On second thought, that's not really very obvious from the
documentation. The trap should be documented. constant.pm documents it
but perlop does not.

Patch attached. While I was at it I also added some examples of useful
uses of =>.

The "=>" operator is a synonym for the comma, but forces any word (con-
sisting entirely of word characters) to its left to be interpreted as a
string (as of 5.001). This includes words that might otherwise be con-
sidered a constant or function call.

  use constant FOO => "something";

  my %h = ( FOO => 23 );

This is equivalent to​:

  my %h = ("FOO", 23);

it is NOT​:

  my %h = ("something", 23);

If the argument on the left is not a word, it is first interpreted as
an expression, and then the string value of that is used.

The "=>" operator is helpful in documenting the correspondence between
keys and values in hashes, and other paired elements in lists.

  %hash = ( key => $value );
  login( $username => $password );

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2005

From @schwern

perlop.patch
--- pod/perlop.pod	2005/07/14 23:48:00	1.1
+++ pod/perlop.pod	2005/07/14 23:54:38
@@ -689,12 +689,29 @@
 
 The C<< => >> operator is a synonym for the comma, but forces any word
 (consisting entirely of word characters) to its left to be interpreted
-as a string (as of 5.001).  If the argument on the left is not a word,
-it is first interpreted as an expression, and then the string value of
-that is used.
+as a string (as of 5.001).  This includes words that might otherwise be
+considered a constant or function call.
+
+    use constant FOO => "something";
+
+    my %h = ( FOO => 23 );
+
+This is equivalent to:
+
+    my %h = ("FOO", 23);
+
+it is I<NOT>:
+
+    my %h = ("something", 23);
+
+If the argument on the left is not a word, it is first interpreted as
+an expression, and then the string value of that is used.
 
 The C<< => >> operator is helpful in documenting the correspondence
 between keys and values in hashes, and other paired elements in lists.
+
+        %hash = ( key => $value );
+        login( $username => $password );
 
 =head2 List Operators (Rightward)
 

@p5pRT
Copy link
Author

p5pRT commented Jul 14, 2005

@schwern - Status changed from 'resolved' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jul 15, 2005

From rick@bort.ca

On Thu, Jul 14, 2005 at 04​:47​:10PM -0700, Michael G Schwern via RT wrote​:

*Any* word, including one that might also be a constant, is interpreted
as a string. %h = ( CONSTANT() => 42 ); is a work around.

Another workaround, since they're constants, is

  %h = (CONSTANT, 42);

--
Rick Delaney
rick@​bort.ca

@p5pRT
Copy link
Author

p5pRT commented Jul 15, 2005

From @steve-m-hay

Michael G Schwern via RT wrote​:

[schwern - Thu Jul 14 16​:47​:07 2005]​:

=> is documented as follows​:

The => digraph is mostly just a synonym for the comma operator. It's
useful for documenting arguments that come in pairs. As of release
5.001, it also forces any word to the left of it to be interpreted as a
string.

*Any* word, including one that might also be a constant, is interpreted
as a string. %h = ( CONSTANT() => 42 ); is a work around.

Reopening this bug.

On second thought, that's not really very obvious from the
documentation. The trap should be documented. constant.pm documents it
but perlop does not.

Patch attached.

Thanks. Applied as change 25149 with

%hash = ( key => $value );

changed to

%hash = ( $key => $value );

(which I think is what you intended given the context.)


Radan Computational Ltd.

The information contained in this message and any files transmitted with it are confidential and intended for the addressee(s) only. If you have received this message in error or there are any problems, please notify the sender immediately. The unauthorized use, disclosure, copying or alteration of this message is strictly forbidden. Note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Radan Computational Ltd. The recipient(s) of this message should check it and any attached files for viruses​: Radan Computational will accept no liability for any damage caused by any virus transmitted by this email.

@p5pRT
Copy link
Author

p5pRT commented Jul 15, 2005

From @schwern

On Fri, Jul 15, 2005 at 10​:33​:36AM +0100, Steve Hay wrote​:

Thanks. Applied as change 25149 with

%hash = ( key => $value );

changed to

%hash = ( $key => $value );

(which I think is what you intended given the context.)

Good point.

--
Michael G Schwern schwern@​pobox.com http​://www.pobox.com/~schwern
Just call me 'Moron Sugar'.
  http​://www.somethingpositive.net/sp05182002.shtml

@p5pRT
Copy link
Author

p5pRT commented Jul 15, 2005

From @steve-m-hay

Now better documented in bleadperl.

@p5pRT
Copy link
Author

p5pRT commented Jul 15, 2005

@steve-m-hay - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant