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

(Updated and clarified) The following endpoint:

https://public-api.wordpress.com/rest/v1/read/feed/?url=the-art-of-aut +ism.com
contains a "feed" url:

https://public-api.wordpress.com/rest/v1/read/feed/34259929

that I want to read.

The following code (based on this JSON Tutorial) gives an error "Use of uninitialized value $feedurl in print".

use strict; use warnings; use Mojo::UserAgent; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/?url=the-art-of-au +tism.com'; my $ua = Mojo::UserAgent->new; my $feedurl = $ua->get( $url )->result->json->{'feeds.meta.links.feed' +}; print $feedurl;

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

Replies are listed 'Best First'.
Re^3: Running JavaScript from within Perl (or just use the API)
by marto (Cardinal) on Sep 20, 2019 at 22:49 UTC

    What happened when you tried to adapt one of the previous examples you've been given?

      I was previously given the example:

      my $subscribers = $ua->get( $url )->result->json->{subscribers_count};

      which I modified (based on this JSON Tutorial) to:

      my $feedurl = $ua->get($url)->result->json->{feeds.meta.links.feed};

      which gave error message:

      Bareword "feeds" not allowed while "strict subs" in use

      I'll keep exploring but would appreciate any hints.

        my $feedurl = $ua->get($url)->result->json->{feeds.meta.links.feed}; which gave error message: Bareword "feeds" not allowed while "strict subs" in use

        Only very simple barewords are automatically quoted in $hash{...} keys, the . is seen as an operator here. Try ...->json->{'feeds.meta.links.feed'};.

        Update: See Re: hash key.

        Lets take it back a step, there's various ways to achieve what you want. Perhaps this example is clearer:

        #!/usr/bin/perl use strict; use warnings; use Mojo::UserAgent; my $url = 'https://public-api.wordpress.com/rest/v1/read/feed/?url=the +-art-of-autism.com'; my $ua = Mojo::UserAgent->new; my $json = $ua->get( $url )->res->json; for my $feed ( @{$json->{feeds}} ){ if ( $feed->{meta}{links}{feed} ){ print "$feed->{meta}{links}{feed}\n"; } }

        For each entry in feeds, print the value of meta->links->feed.

        Update: see Re^4: Running JavaScript from within Perl (or just use the API).

      marto, with regards to your reply on Sep 21, 2019 at 16:31 UTC (which I couldn't figure out how to reply to directly): the code worked (and I'll use it) but gave a warning of "Use of uninitialized value in concatenation (.) or string" at the following line:

      print "$feed->{meta}{links}{feed}\n";

      Thanks for solving the problem, albeit with a confusing warning.

        You've edited your site and the JSON now looks like:

        { "feeds": [ { "subscribe_URL": "https:\/\/the-art-of-autism.com\/feed\/", "feed_ID": "34259929", "meta": { "links": { "feed": "https:\/\/public-api.wordpress.com\/rest\/v1\/read\ +/feed\/34259929", "site": "https:\/\/public-api.wordpress.com\/rest\/v1\/read\ +/sites\/57595012" } } }, { "subscribe_URL": "https:\/\/the-art-of-autism.com\/sample-page-2 +\/feed\/", "feed_ID": "", "meta": { "links": { } } }, { "subscribe_URL": "http:\/\/the-art-of-autism.com\/feed\/", "feed_ID": "34259929", "meta": { "links": { "feed": "https:\/\/public-api.wordpress.com\/rest\/v1\/read\ +/feed\/34259929", "site": "https:\/\/public-api.wordpress.com\/rest\/v1\/read\ +/sites\/57595012" } } } ] }

        Note Sample page 2 has no links. A fix for the code:

        my $json = $ua->get( $url )->res->json; for my $feed ( @{$json->{feeds}} ){ if ( $feed->{meta}{links}{feed} ){ print "$feed->{meta}{links}{feed}\n"; } }

        "which I couldn't figure out how to reply to directly" If viewing a node directly you'll see a link reading "Comment on {Node title here}", if viewing in thread mode you'll see a link titled "[reply]" at the bottom right of each reply.

Re^3: Running JavaScript from within Perl (or just use the API)
by anautismobserver (Sexton) on Sep 21, 2019 at 15:17 UTC

    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.

      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?