在Perl中解码JSON

use*_*409 0 perl json decoding

我有一个像这样的JSON文件(它是整个JSON文件的一部分):

{
  id => "mgp1310",
  samples => [
    {
      envPackage => {
        data => {
          diss_carb_dioxide => {
            aliases    => ["sediment_diss_carb_dioxide"],
            definition => "concentration of dissolved carbon dioxide",
            mixs       => 1,
            required   => 0,
            type       => "text",
            unit       => "",
            value      => "17 mM",
          },
        },
        id => "mge64559",
      },
    },
  ],
}
Run Code Online (Sandbox Code Playgroud)

这已由模块JSON解码,使用:

use Data::Dumper;
use JSON;

open($fh, '<', 'hola.txt' );
$json_text   = <$fh>;
$perl = decode_json($json_text);
print Dumper($perl);  
Run Code Online (Sandbox Code Playgroud)

现在我知道$perl有一个哈希.所以我想使用打印idJSON文件print $perl{"id"};.然而,它打印什么我不知道为什么.

use*_*409 5

use strict在代码中添加了答案.它引发了以下错误:

Global symbol "%perl" requires explicit package name at json.pl line 12.
Run Code Online (Sandbox Code Playgroud)

变量$perl是标量,而不是哈希!当然......我没想过.所以我无法访问哈希写作$perl{"id"}.正确的方法是$perl->{id}.