[ resending because of BS 20k limit ]

Hi Meir,

The problem in the example you gave is in the way you declare your hash.

The operator '=>' is the equivalent of a comma, except that it also 
quotes it's left-hand operand if it is a bareword.
So FIRST => 'foo' is the same as writing 'FIRST' => 'foo' - what you 
actually want to do is disambiguate that FIRST is a constant, and since 
these "constants" are actually just functions that return the value you 
defined, you can do so by replacing FIRST with FIRST(), like Scott 
suggested.

Or you could declare your hash using commas, which don't have the 
magical quoting behaviour of  '=>' :

my %hash = (

   FIRST  , 'one',

   SECOND , 'two',

   THIRD  , 'three',
   );

- Dotan

On 02/28/2011 12:21 PM, Meir Guttman wrote:
 >
 > Hey folks!
 >
 >
 >
 > I discovered the hard way (is there ever an easy way???) that 
declaring constants and then using these as hash keys does not achieve 
the expected (by me, just by me!) results. Rather that using the 
constant _value_ as the hash key, it uses the constant _name_!
 >
 > Arrays behave as expected (by me…) though.
 >
 >
 >
 > Is that a bug or a feature? Why isn't it consistent: use either the 
constants name in both arrays and hashes or  their value, right?
 >
 >
 >
 > Here is a small script to demonstrate the claim. Does your result vary?
 >
 >
 >
 > use strict;
 >
 >
 >
 > use constant FIRST  => "1'st";
 >
 > use constant SECOND => "2'nd";
 >
 > use constant THIRD  => "3'rd";
 >
 >
 >
 > my %hash = (
 >
 >   FIRST  => 'one',
 >
 >   SECOND => 'two',
 >
 >   THIRD  => 'three',
 >
 >   );
 >
 >


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to