Why doesn't Perl support interpolation of hashes in double quotes?

后端 未结 3 1243
不思量自难忘°
不思量自难忘° 2021-01-02 13:31
#!/usr/bin/perl
use warnings;

my %hash=(\"no1\"=>1, 
        \"no2\"=>2,
      );

print %hash; #Prints no11no22
print \"%hash\"; #Prints %hash
3条回答
  •  鱼传尺愫
    2021-01-02 14:25

    Not really an answer to the "why", but I thought I would point out various answers to the "how".

    One could, of course, try:

    #!/usr/bin/perl
    use warnings; use strict;
    
    my %hash = (
        "no1" => 1,
        "no2" => 2,
    );
    
    print "@{[ %hash ]}\n";
    

    But, I don't know what use that would be.

    If you want to dump the contents of a hash or any other complicated data structure, use Data::Dumper or YAML or JSON depending on your use case.

提交回复
热议问题