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

Hello fellow mongers..

I really want to call my variable $run{POWER-NODE} = yes and I can't when running with strict. What is the reasoning behind this, and better yet how can I get around this.
Thanks

Replies are listed 'Best First'.
Re: Hash table key names with a
by particle (Vicar) on Mar 24, 2002 at 15:18 UTC
    barewords are not allowed. use quotes  $run{POWER-NODE} = "yes" or $run{'POWER-NODE'} = "yes"

    ~Particle ;Þ

      Normally, the {} would act as an implicit quote around your key.

      But POWER-MODE looks like a subtraction to the parser, so that messes things up. You'll have to quote it.
      --
      Mike

Re: Hash table key names with a
by Juerd (Abbot) on Mar 24, 2002 at 22:16 UTC

    Valid unquoted strings, such as bare literal hash keys, consist of \w characters only. They are: A-Za-z0-9 and underscore (_). You can have leading or trailing whitespace, which can come in handy. Hoewever, the - character is not allowed, and forces it to be a normal expression.

    Without strict, it's allowed, because without strict it's allowed to use unquoted strings everywhere, not only in literal hash keys and the left operand of the => operator.

    # Assuming you don't have subs called POWER and/or NODE. no strict; $run{POWER-NODE} = yes; # equals: $run{ 'POWER' - 'NODE' } = 'yes'; # equals: $run{ 0 - 0 } = 'yes'; # and thus: $run{0} = 'yes';
    Consider:
    $foo{99-24} = "Hello, world!\n"; print $foo{75};


    With strict, you can't use the unquoted strings POWER, NODE and yes. You'll have to add quotes as such: $run{'POWER-NODE'} = 'yes'. When you get rid of the -, and use an underscore instead, you can use an unquoted string after all (strict makes an exception for =>'s left operand, and for literal constant hash keys) like this: $run{POWER_NODE} = 'yes'.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk