jai has asked for the wisdom of the Perl Monks concerning the following question:

pardon my ignorance, but is there any real difference between having keys of hashes like
%hash = ('key1'=>'value1')
and
%hash = (key1=>'value1'). Is there any performance issues related to this ?
thanks, jai..

Replies are listed 'Best First'.
Re: regarding hash keys
by broquaint (Abbot) on Sep 09, 2003 at 15:12 UTC
    The => operator just stringifies the left-hand side operand and in the case of a bareword forces it to be a string, so both examples equate to the same thing. In terms of performance the difference between the two examples would be negligble in all but the most pathological of cases, and even then the difference would only be noticeable at compile-time.
    HTH

    _________
    broquaint

Re: regarding hash keys
by jeffa (Bishop) on Sep 09, 2003 at 15:18 UTC
    The only reason you need to explicitly quote the Left Hand Side of the => operator is if the key contains white space*:
    my %hash = ('key 1' => 'value1');
    Off-Topic:
    I have to note that PHP also has the arrow operator and it works in the same mannor. However, PHP is "broken" in the sense that you have to quote any keys that are reserved words. For example:
    $hash = array( title => 'Hello World', 'class' => 'this key has to be explicitly quoted', );
    Also note that PHP allows trailing commas, but not in a function parameter list. Crazy.

    UPDATE: silly /me - you need to quote keys that contain \W (that is, non alpha-numeric). Thanks liz and Anonymous Monk. :)

    UPDATE 2: thanks for catching that YuckFoo

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Almost the only reason ;-)

      use strict; my %a = ( a::b => 1 );
      gives:
      Bareword "a::b" not allowed while "strict subs" in use at line 2
      
      This error occurs as far back as 5.00503 and as recent as 5.8.1.

      Liz

      Well, if any of your characters are operators then the keys need to be quoted as well:
      my %hash = ( part-1 => 'once upon a time', part-2 => 'in a galaxy far far away'); print keys %hash;
      > you need to quote keys that contain \W

      Seems you also need to quote keys that have leading digits followed by non-digits.

      my %hash = ( 1 => 'foo', # ok key1 => 'bar', # ok 1key => 'baz', # error );

      YuckFoo

        Not always :-)

        %h = (1e3 => 'foo'); print keys %h;
        1000
Re: regarding hash keys
by hardburn (Abbot) on Sep 09, 2003 at 15:10 UTC

    No, they both compile down to the same thing.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: regarding hash keys
by blue_cowdawg (Monsignor) on Sep 09, 2003 at 15:14 UTC

    Compilation wise or performance wise there is no difference. If you have warnings turned on though you may occasionally get warnings about certain keys in bareword form that may break in the future with later revisions of Perl. I am a loss to think of an example right now.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.