Take the 2-minute tour ×

Im trying to parse the Facebook Graph API JSON results and Im having a bit of trouble with it.

what I was hooping to do was print the amount of shares -

my $trendsurl = "https://graph.facebook.com/?ids=http://www.filestube.com";
my $json;
  local $/; #enable slurp
  open my $fh, "<", $trendsurl;
  $json = <$fh>;

my $decoded_json = @{decode_json{shares}};
print $decoded_json;
share|improve this question
add comment

2 Answers

Some of the code above is extremely puzzling. I've just rewritten it with annotations for you.


use LWP::Simple;                # From CPAN
use JSON qw( decode_json );     # From CPAN
use Data::Dumper;               # Perl core module
use strict;                     # Good practice
use warnings;                   # Good practice


# open is for files.  unless you have a file called
# 'https://graph.facebook.com/?ids=http://www.filestube.com' in your
# local filesystem, this won't work.
#  local $/; #enable slurp
#  open my $fh, "<", $trendsurl;
#  $json = <$fh>;

# 'get' is exported by LWP::Simple; install LWP from CPAN unless you have it.
# You need it or something similar (HTTP::Tiny, maybe?) to get web pages.
my $json = get( $trendsurl );
die "Could not get $trendsurl!" unless defined $json;

# This next line isn't Perl.  don't know what you're going for.
#my $decoded_json = @{decode_json{shares}};

# Decode the entire JSON
my $decoded_json = decode_json( $json );

# you'll get this (it'll print out); comment this when done.
print Dumper $decoded_json;

# Access the shares like this:
print "Shares: ",

Run it and check the output. You can comment out the print Dumper $decoded_json; line when you understand what's going on.

share|improve this answer
Great +1 response. I have not written much perl code in 15 years, so your thorough example really helped me get back up to speed on perl. –  fool4jesus Mar 27 '13 at 14:08
add comment

How about using CURL command instead? (P.S.: This is running on Windows; make CURL changes for Unix systems).

   $curl=('C:\\Perl64\\bin\\curl.exe -s http://graph.facebook.com/?ids=http://www.filestube.com');
    print "Output is::: \n$exec\n\n";

    ## match the string "shares": in the CURL output
    if ($exec=~/"shares":?/)
        ## string after the match (any string on the right side of "shares":)
        ## delete all non-Digit characters after the share number
        print "Number of Shares is: ".$shares."\n";
    } else {
        print "No Share Information available.\n"


Output is::: {"http://www.msn.com":{"id":"http://www.msn.com","shares":331357,"comments":19}}

Number of Shares is: 331357

share|improve this answer
Using CURL is not exactly using perl. –  Jacob Feb 3 at 7:50
add comment

Your Answer


