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

前端 未结 9 1752
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

    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 );
    

提交回复
热议问题