in reply to Re^2: Running JavaScript from within Perl
in thread Running JavaScript from within Perl

In response to your short example using Mojo::UserAgent: (which I couldn't figure out how to respond to directly):

I modified your code as follows to read url's from a file:

use strict; use warnings; use Mojo::UserAgent; my $filename = 'urls_Mojo.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $y = 0; # input row count while (my $row = <$fh>) { $y++; print $y; print " $row"; my $url = $row; # create a Mojo:UserAgent my $ua = Mojo::UserAgent->new; # use $ua to get the url and assign the value of 'subscriber_count' in + the json # to avariable, $subscribers my $subscribers = $ua->get( $url )->result->json->{subscribers_count}; # print the variable to screen print "Number of subscribers: $subscribers\n"; }

it worked when the file 'urls_Mojo.txt' contained

https://public-api.wordpress.com/rest/v1/read/feed/http%3A%2F%2Fthe-ar +t-of-autism.com%2Ffeed

but gave a "Can't use an undefined value as a HASH reference" error when I added a second line to 'urls_Mojo.txt' as follows:

https://public-api.wordpress.com/rest/v1/read/feed/http%3A%2F%2Fthe-ar +t-of-autism.com%2Ffeed https://public-api.wordpress.com/rest/v1/read/feed/http%3A%2F%2Fanauti +smobserver.wordpress.com%2Ffeed

Can you help me figure out how to apply this script to a list of url's in a file? Thanks.

Replies are listed 'Best First'.
Re^4: Running JavaScript from within Perl
by hippo (Archbishop) on Sep 18, 2019 at 22:08 UTC

    You have not chomped $row (as can be seen from the printed output when run). If I do that it works fine.

    $ diff 11106348_orig.pl 11106348.pl 12a13 > chomp $row;

    I presume when you used just one URL in the data file you did not even add the EOL character so it worked as a fluke.

Re^4: Running JavaScript from within Perl
by marto (Cardinal) on Sep 19, 2019 at 08:53 UTC