-2

I'm pretty basic at Perl, and I need to make a project for my university.

I want to download data from certain link, it is JSON data, so I know I have to use JSON::Parse module from CPAN.

But how to download content of link to my Perl variable? Should I use LWP get()?

Borodin
  • 126,100
  • 9
  • 70
  • 144

1 Answers1

3

Aren't you supposed to be learning Perl if it's a university project?

Anyway, your program will look something like this. It uses the LWP::Simple module to fetch the JSON data, and then JSON::Parse to process it into a Perl data structure

I've used printed the author value from each item of the array, as requested in your comment

use strict;
use warnings 'all';

use LWP::Simple 'get';
use JSON::Parse 'parse_json';

use constant URL => 'http://a.wykop.pl/observatory/entries/appkey,dJ4w7fXYpL';

my $json = get URL or die "Unable to get JSON data";
my $data = parse_json($json);

print "Authors:\n";
print "  $_->{author}\n" for @$data;

output

Authors:
  Snurq
  AferaZaAfera
  Devest
  igorsped
  Matt23
  labla
  poprawnie-niepoprawny
  Parkero
  Speed666
  Xune
  Gasior9
  mikolajeq
  Aztek2201
  blogerbezbloga
  Pan_wons
  PanKaczucha
  NieznanyAleAmbitny
  dzika_kaczka_bez_dzioba
  ilili
  Bager
  bmbcz01
  hydrocyfolumpus
  acarter
  Doctor_Manhattan
  strumienzgor
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • I know, that professors should have taught us, but there is nothing I can do. This is perfect answer, however I would like to ask one more question. What should I do, to Data::Dumper doesn't write variable names as "deleted" "id" "date", just content? Or would I like to just print each "author" variable from my $data? – Filip Bartman Jun 07 '16 at 16:16
  • @FilipBartman... if this answer helped you, please check the checkmark next to it to "accept" it. If you want to get only authors: `for (@$data){ print "$_->{author}\n";}` – stevieb Jun 07 '16 at 16:23
  • @FilipBartman: The only variable is `$data`, and you know its name. I've altered my answer to print a list of the authors instead of using `Data::Dumper` to display the whole data structure – Borodin Jun 07 '16 at 17:41
  • @FilipBartman: You can't learn the whole of the Perl language by asking questions one by one; in any case you shouldn't ask further questions in the comments, you need to open a new question. If you get that error message then the data isn't what you think it is, and it's probably an array – Borodin Jun 07 '16 at 18:23
  • @Borodin I have tried to write a second program, I just changed API link to 'http://a.wykop.pl/entries/index/18040011/appkey,dJ4w7fXYpL', now it's not an array, 25 top entries, now it's 1 single entry. I have "Not a HASH " error, in this line print "$data->{author} " – Filip Bartman Jun 07 '16 at 18:24
  • @Borodin unfortunately, I must wait 2 days to open a new question. I've tried print"$data{'author'} but now I see "global symbol %data require explicit package name". It is defined above, I have no clue – Filip Bartman Jun 07 '16 at 18:29
  • @FilipBartman: Using that URL, `print $data->{author}, "\n"` produces `Variv` – Borodin Jun 07 '16 at 18:41
  • @FilipBartman You should use what I wrote. `$data{author}` is looking up element `author` in `%data`, which doesn't exist. You hash a *hash reference* `$data`, so you need to write `print $data->{author}, "\n"`. Now you should go and learn Perl. It isn't fair to press me to make off-topic posts just to help you out, and this is no way to learn a language. [learn.perl.org](http://learn.perl.org/) and [perl-tutorial.org](http://perl-tutorial.org/) are excellent resources, and Stack Overflow has some very useful links on its [perl tag information page](http://stackoverflow.com/tags/perl/info) – Borodin Jun 07 '16 at 18:51
  • @Borodin Thank you very much for your help, I won't press you any more :) – Filip Bartman Jun 07 '16 at 18:54