Why does json_decode return a scalar?

I am looking for help to understand why json_decode returns a scalar instead of a hash. I am still studying perl and the description or some link. it would be great.

So questions: Why does json_decode return a scalar? (or is it not a scalar) Is there a better way to work with data?

Here is my code:

use strict;
use warnings;
use JSON qw(decode_json);
use LWP::UserAgent;

my $url = "http://api.bf4stats.com/api/playerInfo?plat=xbox&name=Ttylz00&output=json";

my $ua = LWP::UserAgent->new;
my $data = $ua->get($url);
my $json;
if($data->is_success){
    $json = decode_json($data->decoded_content);
}

&sData($json);

sub sData {
    my $data = $_[0];
    my $kdr = int($data->{stats}->{extra}->{kdr}*100)/100;
    printf "\nName: %s\nRank: %s, %s\nKDR: %s\n", $data->{player}->{name}, 
        $data->{player}->{rank}->{nr}, $data->{player}->{rank}->{name}, 
        $kdr;
}
+1
source share
1 answer

In Perl, a function can only really return a scalar or list.

(, %foo = (a => 1, b => 2)), , , json_decode - { a => 1, b => 2 } ( ), (a => 1, b => 2) (a , ).

:

  • Perl . , - { "a": { "b": 3 } }, { "b": 3 } ; , .
  • ( ), , , .
  • JSON (= Perl), (= Perl). json_decode , , . JSON, , , ? ( %foo = json_decode(...), , .) , json_decode , , , .
+2

All Articles