in reply to Re^2: Running JavaScript from within Perl (or just use the API)
in thread Running JavaScript from within Perl

The following code works to assign $subscribers to subscribers_count, but gives an error "Use of uninitialized value $feedurl in print" for the assignment of $feedurl.

use strict; use warnings; use Mojo::UserAgent; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/34259929'; my $ua = Mojo::UserAgent->new; my $subscribers = $ua->get($url)->result->json->{subscribers_count}; print "Number of subscribers: $subscribers\n"; my $feedurl = $ua->get( $url )->result->json->{'meta.links.self'}; print $feedurl;

Pleae tell me what I'm doing wrong. Thanks.

Replies are listed 'Best First'.
Re^4: Running JavaScript from within Perl (or just use the API)
by hippo (Archbishop) on Sep 21, 2019 at 22:14 UTC

    There is no element named "meta.links.self" in the returned JSON, so when you try to set $feedurl to that it is unititialised.

    Also, you call $ua->get($url)->result->json twice in the space of 3 lines. DRY.

    use strict; use warnings; use Mojo::UserAgent; use Data::Dumper; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/34259929 +'; my $ua = Mojo::UserAgent->new; my $json = $ua->get($url)->res->json; print Dumper ($json); my $subscribers = $json->{subscribers_count}; print "\nNumber of subscribers: $subscribers\n"; my $feedurl = $json->{meta}{links}{self}; print "URL is $feedurl\n";

    This works for me. My version of Mojo::UserAgent (Mojolicious 5.77) does not have a result method. YMMV.

      I responded in error (figured out my mistake). How do I delete my response?

        How do I delete my response?

        You shouldn't, perhaps only <strike> out the things no longer relevant and mark edits with "Update". See How do I change/delete my post?