:) , , , , , .
use strict;
use warnings;
use JSON;
my $json_str = ' { "items" : [ 1, 2, 3, 4 ] } ';
my $content = decode_json($json_str);
:
, , .
, , decode_json , , . ( )
UTF-8 () JSON UTF-8, .
my $content = decode_json($json_str);
SCALAR ( ).
: , :
printf "reftype:%s\n", ref($content);
hashref -
print "key: $_\n" for keys %{$content};
"items" (arrayref)
my $aref = $content->{items};
#my $aref = $content{items}; #throws error if "use strict;"
#Global symbol "%content" requires explicit package name at script.pl line 20.
$content{item} %content, / . $content - , - %content.
{
use 5.020;
use experimental 'postderef';
print "key-postderef: $_\n" for keys $content->%*;
}
- arrayref -
printf "reftype:%s\n", ref($aref);
print "arr-item: $_\n" for @{$aref};
{
use 5.020;
use experimental 'postderef';
print "aref-postderef: $_\n" for $aref->@*;
}
:
my @arr;
my $arr_ref = \@arr;
@{$arr_ref} is the same as @arr
^^^^^^^^^^ - array reference in curly brackets
$arrayref - @{$array_ref} , .
my %hash;
my $hash_ref = \%hash;
%{$hash_ref} is the same as %hash
^^^^^^^^^^^ - hash reference in curly brackets
$hash_ref - %{$hash_ref} , .
say $content->{items}->[0];
say $content->{items}[0];
say ${$content}{items}->[0];
say ${$content}{items}[0];
say ${$content->{items}}[0];
say ${${$content}{items}}[0];
1.