Are quotes around hash keys a good practice in Perl?

后端 未结 13 2071
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 06:24

Is it a good idea to quote keys when using a hash in Perl?

I am working on an extremely large legacy Perl code base and trying to adopt a lot of the best practices s

13条回答
  •  清歌不尽
    2020-12-03 07:09

    I've wondered about this myself, especially when I found I've made some lapses:

     use constant CONSTANT => 'something';
     ...
     my %hash = ()
     $hash{CONSTANT}          = 'whoops!';  # Not what I intended
     $hash{word-with-hyphens} = 'whoops!';  # wrong again 
    

    What I tend to do now is to apply quotes universally on a per-hash basis if at least one of the literal keys needs them; and use parentheses with constants:

     $hash{CONSTANT()} = 'ugly, but what can you do?';
    

提交回复
热议问题