Is there any way to use a “constant” as hash key in Perl?

前端 未结 9 1721
Happy的楠姐
Happy的楠姐 2021-02-03 20:39

Is there any way to use a constant as a hash key?

For example:

use constant X => 1;

my %x = (X => \'X\');

The above code will cr

相关标签:
9条回答
  • 2021-02-03 21:14

    Most of the other folks have answered your question well. Taken together, these create a very full explanation of the problem and recommended workarounds. The issue is that the Perl pragma "use constant" really creates a subroutine in your current package whose name is the the first argument of the pragma and whose value is the last.

    In Perl, once a subroutine is declared, it may be called without parens.

    Understanding that "constants" are simply subroutines, you can see why they are not interpolated in strings and why the "fat comma" operator "=>" which quotes the left-hand argument thinks you've handed it a string (try other built-in functions like time() and keys() sometime with the fat comma for extra fun).

    Luckily, you may invoke the constant using explicit punctuation like parens or the ampersand sigil.

    However, I've got a question for you: why are you using constants for hash keys at all?

    I can think of a few scenarios that might lead you in this direction:

    1. You want control over which keys can be in the hash.

    2. You want to abstract the name of the keys in case these change later

    In the case of number 1, constants probably won't save your hash. Instead, consider creating an Class that has public setters and getters that populate a hash visible only to the object. This is a very un-Perl like solution, but very easily to do.

    In the case of number 2, I'd still advocate strongly for a Class. If access to the hash is regulated through a well-defined interface, only the implementer of the class is responsible for getting the hash key names right. In which case, I wouldn't suggest using constants at all.

    Hope this helps and thanks for your time.

    0 讨论(0)
  • 2021-02-03 21:14

    One way is to encapsulate X as (X):

    my %x ( (X) => 1 );
    

    Another option is to do away with '=>' and use ',' instead:

    my %x ( X, 1 );
    
    0 讨论(0)
  • 2021-02-03 21:14

    Comment @shelfoo (reputation not high enough to add comment directly there yet!)

    Totally agree about Perl Best Practices by Damian Conway... its highly recommended reading.

    However please read PBP Module Recommendation Commentary which is a useful "errata" if you plan to use PBP for an in-house style guide.

    0 讨论(0)
  • 2021-02-03 21:16

    => operator interprets its left side as a "string", the way qw() does.

    Try using

    my %x = ( X, 'X');
    
    0 讨论(0)
  • 2021-02-03 21:18

    The use constant pragma creates a subroutine prototyped to take no arguments. While it looks like a C-style constant, it's really a subroutine that returns a constant value.

    The => (fat comma) automatically quotes left operand if its a bareword, as does the $hash{key} notation.

    If your use of the constant name looks like a bareword, the quoting mechanisms will kick in and you'll get its name as the key instead of its value. To prevent this, change the usage so that it's not a bareword. For example:

    use constant X => 1;
    %hash = (X() => 1);
    %hash = (+X => 1);
    $hash{X()} = 1;
    $hash{+X} = 1;
    

    In initializers, you could also use the plain comma instead:

    %hash = (X, 1);
    
    0 讨论(0)
  • 2021-02-03 21:19

    use constant actually makes constant subroutines.

    To do what you want, you need to explicitly call the sub:

    use constant X => 1;
    
    my %x = ( &X => 'X');
    

    or

    use constant X => 1;
    
    my %x = ( X() => 'X');
    
    0 讨论(0)
提交回复
热议问题