Can't run Perl script on other computer - perl

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

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:
print $response->content to see the full error message.
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.

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

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 );
}

For the record, this fixed the issue for me (CentOS):
# yum install perl-Crypt-SSLeay

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 )

Related

Getting "ViCommand: no such keymap" errors while using Term::Readline:Zoid

I have a simple bit of code that works well until I export
PERL_RL='Zoid default_mode=ViCommand'
in order to use vi mode while editing. When I test the code I'm getting the error: ViCommand: no such keymap. I installed libterm-readline-zoid-perl and from what I can tell ViCommand is built in so I should be good to go but apparently not.
#!/usr/bin/env perl
use warnings;
use strict;
use Term::ReadLine;
my $term = new Term::ReadLine 'LineEdit';
while ( defined ($_ = $term->readline($ARGV[0],$ARGV[1])) ) {
print $_;
exit;
}
Seems like there is no command named ViCommand. However, by inspecting the source, see line 80:
command => { _use => 'Term::ReadLine::Zoid::ViCommand' },
you should be able to use "ViCommand" line editing by setting the environment variable PERL_RL like this:
PERL_RL='Zoid default_mode=command'
Another solution:
$term->parse_and_bind("set editing-mode vi");

Perl url encoding using Curl

I am having an issue where I am using cURL inside a perl script to execute a http request. I believe my issue is related to special characters in the URL string but I cannot figure out how to make it work.
I can confirm that the URL is correct as I can run it from my browser.
My perl script is
#!/usr/bin/perl
use strict;
use warnings;
$url = "http://machine/callResync?start=2017-02-01 00.00.00.000&end=2017-02-01 23.23.999";
system "curl $url
It fails when it reaches the first whitespace. I tired to escape that using %20.
After that I put in %26 to escape the & but then I get another issue. I have tired a number of different combinations but it keeps failing.
Any idea's.
Use the URI module to correctly build a URL, and rather than shelling out to cURL you should use a Perl library like LWP::Simple to access the page
The disadvantage of LWP::Simple is that it may be too simple in that it provides no diagnostics if the transaction fails. If you find you need something more elaborate then you should look at
HTTP::Tiny,
LWP::UserAgent, or
Mojo::UserAgent.
If you need help with these then please ask
use strict;
use warnings 'all';
use URI;
use LWP::Simple 'get';
my $url = URI->new('http://machine/callResync');
$url->query_form(
start => '2017-02-01 00.00.00.000',
end => '2017-02-01 23.23.999',
);
my $content = get($url) or die "Failed to access URL";
Problem number 1: You used an invalid URL. Spaces can't appear in URLs.
my $url = "http://machine/callResync?start=2017-02-01%2000.00.00.000&end=2017-02-01%2023.23.999";
Problem number 2: Shell injection error. You didn't correctly form your shell command.
system('curl', $url);
or
use String::ShellQuote qw( shell_quote );
my $cmd = shell_quote('curl', $url);
system($cmd);

Perl SSH Script unable to load math library?

I am trying to run a Perl script that takes user, pass, ip arguments and uses that to check the version of a network switch through ssh. However when i run it on our server i keep getting:
Math::BigInt: couldn't load specified math lib(s), fallback to Math::BigInt::FastCalc at /usr/lib/perl5/site_perl/5.10.0/Crypt/DH.pm line 6
It the proceeds to hang for a bit then return with no output. What is causing this to fail and how can i get around it? I do not have access to install extra modules to the server.
EDIT: I have checked the currently installed modules and Net::SSH:Perl, Math::BigInt::FastCalc, and Math::Pari are all installed, so i have no idea why it is having problems loading those modules.
Here is my script for reference:
#!/usr/bin/perl
# Outputs the name of the Flash File (which denotes the software version) of the switch
#Input: User Pass Host
open(FH, ">>unparsed.txt");
open (FH2, ">>versions.txt");
use strict;
use Net::SSH::Perl;
my $user = $ARGV[0];
my $pass = $ARGV[1];
my $host = $ARGV[2]; #Hostname given as command line argument
my $cmd = "show version";
my $version;
print("\n");
print($ARGV[2]);
print("\n");
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass); # login to switch
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
printf FH ($stdout); #output all test to file
close(FH);
open(FH, "unparsed.txt");
while(<FH>){ #look through file for flash filename
if($_ =~ /System image file is "(.*)"/){
$version = $1;
}
}
print ($version); #output flash filename
print ("\n");
printf FH2 ($ARGV[2]);
printf FH2 ("\n");
printf FH2 ($version);
printf FH2 ("\n");
close(FH2);
close(FH);
Crypt::DH loads Math::BigInt with:
use Math::BigInt lib => "GMP,Pari";
Therefore, you need either GMP or Pari on your system.
Your distribution's package manager may already provide a means to install them.
Have you tried using Net::SSH?
I prefer Net::SSH, and Net::SFTP::Foreign, math libs usally give me troubles when I try to install them on older systems (specially with the mess that some sysadmin do with paths on Unix systems). Most systems either use OpenSSH, or some fork of it (like SunSSH on Solaris), so it's less likely that you you'll have any sort of trouble using those distributions.
Try using Net::OpenSSH instead of Net::SSH::Perl.

regex /ms not behaving as expected perl 5.8.8

I have a failing test on 5.8.8, I don't understand why, esp when it works in more recent versions (perhaps it was just a bug) (here's a link to the full code)
use strict;
use warnings;
use Test::More;
my $fname = 'Fo';
my $content = do { local $/ ; <DATA> };
like $content, qr/^$fname $/xms, q[includes first name];
done_testing;
__DATA__
use strict;
use warnings;
use Test::More;
# generated by Dist::Zilla::Plugin::Test::PodSpelling bootstrapped version
eval "use Test::Spelling 0.12; use Pod::Wordlist::hanekomu; 1" or die $#;
add_stopwords(<DATA>);
all_pod_files_spelling_ok('bin', 'lib');
__DATA__
Fo
oer
bar
on all recent versions of perl this works fine. but in 5.8.8 the test fails. I found that by removing the ^ and $ the code works, its like Perls regex engine is ignoring the /m but the documentation says it was supported.
Why does this not work? and what is the most correct way to fix it? (note: I believe that the test should check that these elements are on a line by themselves )
This is bug RT#7781. It was fixed in 5.8.9 and 5.10.0.
Workarounds:
qr/^/m is equivalent to qr/(?:^|(?<=\n))/
qr/$/m is equivalent to qr/(?=\n|\z)/

BioPerl & CPAN - Problems in installation & error "Can't locate Bio/EnsEMBL/Registry.pm in #INC"

I'm posting this message out of pure desperation, because I really don't know what else to try. I'm a beginner in bioperl and I'm working on a script to parse out some results I got from MolQuest fgenesh. Results are out in .txt format and I want to parse them to GFF and fasta file for mRNA and protein sequences to facilitate comparison with other results we have. So I found the Bio::Tools::Fgenesh module and I'm working on a script with it. Problem is, BioPerl doesn't seem to work on my ubuntu pc
I followed the instructions here http://www.bioperl.org/wiki/Installing_Bioperl_for_Unix . I managed to install CPAN in root mode (otherwise it wouldn't work) and BioPerl via CPAN. All tests were ok, but when I ran this script to test the installation
use strict;
use warnings;
use Getopt::Long;
use Bio::EnsEMBL::Registry;
my $reg = "Bio::EnsEMBL::Registry";
$reg->load_registry_from_db(
-host => "ensembldb.ensembl.org",
-user => "anonymous"
);
my $db_list=$reg->get_all_adaptors();
my #line;
foreach my $db (#$db_list){
#line = split ('=',$db);
print $line[0]."\n";
}
I got the error: "Can't locate Bio/EnsEMBL/Registry.pm in #INC"
I tried to install BioPerl again via Build.PL, running as root, but still came to the same outcome.
Thanks for your help
Merche
You seem to be attempting to use the Ensembl API. This is not part of the BioPerl distribution. Please see http://www.ensembl.org/info/docs/api/api_installation.html for more information about how to install it. We do not recommend you install this in any of the default Perl library location as the API is heavily tied into the data made in the same release. Ensembl provides 4-5 releases per year so maintaining this can be difficult.
Should you have anymore issues then you can contact the developers. We have an active developers mailing list & a helpdesk. See http://www.ensembl.org/info/about/contact/index.html for more information.
I had ecountered the same error as you, working on a windows 64x. Seems Bio::EnsEMBL::Registry is not recognized on my windows computer.
Following all the ENSEMBL-API instructions, I finally came across a debugging page (http://www.ensembl.org/info/docs/api/debug_installation_guide.html). After running C:\src\ensembl/misc-scripts/ping_ensembl.pl, I got again the same error message as listed above.
According to the PERL API help for windows, I need to run "set PERL5LIB=C:\src\bioperl-1.2.3;C:\src\ensembl\modules;C:\src\ensembl-compara\modules;C:\src\ensembl-variation\modules;C:\src\ensembl-funcgen\modules" from the cmd box. Did that, but error remained the same.
Now I included these paths (C:\src\bioperl-1.2.3;C:\src\ensembl\modules;C:\src\ensembl-compara\modules;C:\src\ensembl-variation\modules;C:\src\ensembl-funcgen\modules) directly in my perl script, and this seems to work. Probably this is not the way to do it, but as long as it works, I'm happy.
See an example script (based on excercises provided by Bert Overduin) below:
#!/usr/bin/perl -w
use lib "C:/src/ensembl/modules";
use lib "C:/src/ensembl/modules/Bio/EnsEMBL";
use lib "C:/src/ensembl-compara/modules/Bio/EnsEMBL/Compara";
use lib "C:/src/ensembl-functgenomics/modules/Bio/EnsEMBL/Funcgen";
use lib "C:/src/ensembl-variation/modules/Bio/EnsEMBL/Variation";
use strict;
use Bio::EnsEMBL::Registry;
my $registry = 'Bio::EnsEMBL::Registry';
$registry->load_registry_from_db(
-host => 'ensembldb.ensembl.org',
-user => 'anonymous',
-verbose => '1'
);
my $slice_adaptor =
Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "slice" );
get a slice on the entire chromosome X
my $chr_slice = $slice_adaptor->fetch_by_region( 'chromosome', '13', 32_889_000, >32_891_000 );
print "#######################################################\n";
print $chr_slice->seq;
Or alternatively:
#!/usr/bin/perl -w
BEGIN{ push #INC,'C:/src/bioperl-live','C:/src/ensembl/modules','C:/src/ensembl-compara/modules','C:/src/ensembl-variation/modules','C:/src/ensembl-functgenomics/modules';};
use strict;
use Bio::EnsEMBL::Registry;
my $registry = 'Bio::EnsEMBL::Registry';
$registry->load_registry_from_db(
-host => 'ensembldb.ensembl.org',
-user => 'anonymous',
-verbose => '1'
);
my $slice_adaptor =
Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "slice" );
get a slice on the entire chromosome X
my $chr_slice = $slice_adaptor->fetch_by_region( 'chromosome', '13', 32_889_000, >32_891_000 );
print "#######################################################\n";
print $chr_slice->seq;