in reply to use constant for strings

Besides the parens (around or following) and the ampersand, you can use an unary plus - the Perl way of disambiguating whether you are referring to an expression or something else.
my %capitol_map = ( +WISCONSIN => "Madison", +ILLINOIS => "Springfield", ... );
See rir's post below..

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: use constant for strings (unary plus)
by rir (Vicar) on Apr 18, 2003 at 19:43 UTC
    use constant KEY => "key"; my %h = ( +KEY => "data" , KEY() => "data",); $, = "\n"; print "Perl vers: $]", keys %h;
    Outputs:
    Perl vers: 5.006001 KEY key
      Gack, you're right.

      Makeshifts last the longest.

Re^2: use constant for strings (unary plus)
by particle (Vicar) on Apr 18, 2003 at 18:50 UTC

    personally, i don't like the unary plus. how does it make things less ambiguous? "plus" means "not an expression"? it's not clear. besides, we have sigils to remove ambiguity, anyway. i'd recommend &WISCONSIN, or if you're afraid of side effects (which won't occur with constants,) use &WISCONSIN(). more to write, but leaving less to the imagination.

    ~Particle *accelerates*

      I don't want to use the sigil simply because it disables the prototype and thus the constant-ish nature as mentions of the "constant" become regular runtime function calls (otherwise they'd be substituted at compile time). I might as well just use a variable.

      Seeing as the unary plus doesn't work here, the next best thing I'd prefer would be (WISCONSIN) - with the parens following it looks quite awkward IMHO.

      I like the unary plus in other situations and use it lots though - f.ex

      map +(foo($_, "bar"))[3], @baz;
      where I would otherwise be forced to use curlies
      map { foo($_, "bar"))[3] } @baz;
      or another extra pair of parens.
      map((foo($_, "bar"))[3], @baz);
      I don't like either of the latter two, though I realise quite a few people here will disagree. I think the last one at least is certain not to win any prizes for beauty..

      Makeshifts last the longest.

        Out of interest, why do you favour map +...., @.. over map{ ... } @..?


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.
      i'd recommend &WISCONSIN, or if you're afraid of side effects (which won't occur with constants,)

      Then I guess you've never noticed how "constants" are implemented by default when one uses h2xs. I've seen &CONST cause warnings in real code because of that.

      But I don't use &CONST because I know it passes @_ to the CONST subroutine and I don't want whoever ends up maintaining my code wondering why I'm passing @_ in. Just because it (usually) works, doesn't mean it makes sense. (:

                      - tye