Milos Lazovic
Milos Lazovic

Reputation: 43

Can't run Perl script on other computer

When I try to run script on my second computer I get this message:

malformed JSON string, neither array, object, number, string or atom, at character offset 0
(before "LWP will support htt...") at iptest.pl line 21,  line 2.

On my first computer, the script works fine.

Line 21:

my $data = decode_json($resp->content);

Does anyone know what the problem can be?

Thanks in advance

Upvotes: 0

Views: 1640

Answers (5)

user13660738
user13660738

Reputation: 1

I got in the same situation. After I used 'yum' to install perl, I still got the error when run the perl script.

$ sudo yum install perl             
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management
...

Eventually I did these, and it works.

$ lwp-request https://google.com/

It returned an error message .. (LWP::Protocol::https not installed)

$ sudo rpm -ivh ~/perl-LWP-Protocol-https-6.07-4.el8.noarch.rpm

$ lwp-request https://google.com/

it returned a HTML page.

and my perl script can run without error.

(my system is:

$ cat /etc/os-release PRETTY_NAME=Red Hat Enterprise Linux 8.0 )

Upvotes: 0

Joseph
Joseph

Reputation: 121

For the record, this fixed the issue for me (CentOS):

# yum install perl-Crypt-SSLeay

Upvotes: 1

mokko
mokko

Reputation: 186

I applied most of the changes ikegami suggested. Then perl gave me good error messages to fix the remaining issues. It looks like it works now. Don't ask why it didn't work before. Your code is weird that it's hard to say what exactly went wrong. With strict and warnings you're forced to write better code. Maybe add some nicely named subroutines to add more clarity.

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use LWP::UserAgent;
use open qw(:std :utf8);
use LWP::Simple;
use YAML::Tiny;
use JSON;
use URI;
use List::MoreUtils qw(uniq);

print "Enter Qve:";
my ( $qve, $loc, $key, $href );
chomp( $qve = <STDIN> );
print "Enter Location:";
chomp( $loc = <STDIN> );

$key = '';
my $format = '$format';
$href =
"https://api.datamarket.azure.com/Bing/Search/v1/Web?Query='$qve [loc:$loc]'&Latitude=43&Longitude=19&$format=JSON";
my $ua = LWP::UserAgent->new('keep_alive');
$ua->credentials( "api.datamarket.azure.com" . ':443', '', '', $key );
my $resp = $ua->get($href);
my $data = decode_json( $resp->decoded_content( charset => 'none' ) );
my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} };

my @za;
for my $i ( 0 .. $#urls ) {
    my $trz  = "www.";
    my $host = URI->new( $urls[$i] )->host;
    $host =~ s/$trz//g;
    push( @za, $host );
}

Upvotes: 1

innaM
innaM

Reputation: 47849

I'm a bit surprised that the JSON error is the only error you get. But it does contain a tiny little hint: "LWP will support htt...". I bet that LWP is missing a module it needs to be able to make https connections. You now have two options:

  1. print $response->content to see the full error message.
  2. On the command line, do something like lwp-request https://google.com/. You should see the full error message.

Then install the missing module.

And of course: please, please, please:

  • use strict and use warnings
  • Clean that script up and throw away every use-line you don't need: IO::Socket, LWP::Simple, YAML::Tiny.
  • Read the documentation of the modules that you actually are using. What are you trying to achieve with LWP::UserAgent->new(keep_alive)? Hint: It won't help to quote keep_alive.

Upvotes: 3

ikegami
ikegami

Reputation: 385915

Some issues:

  1. Always use use strict; use warnings;.
  2. Never use $response->content. What it returns is useless. Instead, use $response->decoded_content( charset => 'none').
  3. You need to chomp the values you get from STDIN.
  4. You should never use our unless forced to (e.g. our @ISA = ok). my should be used instead.
  5. my $format = '$format'; "$format" is a silly way of writing "\$format".

Upvotes: 2

Related Questions