Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Running JavaScript from within Perl (or just use the API)

by marto (Cardinal)
on Sep 20, 2019 at 22:49 UTC ( [id://11106455]=note: print w/replies, xml ) Need Help??


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

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

  • Comment on Re^3: Running JavaScript from within Perl (or just use the API)

Replies are listed 'Best First'.
Re^4: Running JavaScript from within Perl (or just use the API)
by anautismobserver (Sexton) on Sep 20, 2019 at 23:23 UTC

    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).

Re^4: Running JavaScript from within Perl (or just use the API)
by anautismobserver (Sexton) on Sep 22, 2019 at 00:09 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found