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

前端 未结 9 1754
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: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');
    

提交回复
热议问题