Re: pl script in webserver
by kcott (Archbishop) on Apr 08, 2017 at 21:12 UTC
|
G'day bigup401,
Some things for you to check:
-
The value of "$res->content".
-
The value of "$respons".
-
"$respons" is a hashref, with a "data" key, whose value is also a hashref, with a "data" key.
-
Why you're assigning the same value to "$qu" and "$je".
-
Whether you really need to "remove all JSON::XS lines" or does the problem go away if you remove just some;
in other words, track down the actual line(s) causing the problem.
-
That you are using the strict and warnings pragmata.
Some things you haven't provided to us:
-
Sample input.
-
Actual output (even if it's "not readable by browser").
-
Expected output.
-
Error or warning messages (++marto already mentioned this).
-
The versions of Perl and JSON::XS that you're using.
-
The encoding of your input data. (Have you tried using methods such as ascii(), latin1(), or utf8().)
You could also check whether the JSON
or JSON::PP modules have the same problems.
Be aware of an incompatibility issue
with JSON and JSON::XS.
Providing an SSCCE, that allows us to exactly reproduce your problem,
will greatly improve your chances of receiving a succinct answer.
| [reply] [d/l] [select] |
Re: pl script in webserver
by huck (Prior) on Apr 08, 2017 at 21:17 UTC
|
Here is a more user friendly version of the use statement tester. You can enter the modules you want to test into a textarea box, instead of having to edit the code to test a new use statement. Took way to long to put together as i kept wondering why my box label still was centered even tho i added style="vertical-align:top;". Of course i added it to the textarea td .....
#!/usr/bin/perl
#
# http://www.perlmonks.org/?node_id=1187481
#
use strict; use warnings;
use CGI;
use HTML::Entities qw/encode_entities/;
my $cgi = CGI->new();
print $cgi->header();
print'<head><title>Perl use statment Testing</title></head><body>'."\n
+" ;
my $uselist0=$cgi->param('uselist');
my $uselist=$uselist0;
$uselist='CGI::Carp JSON' unless (defined $uselist);
$uselist=~s/[\n\r\t\l]+/ /g;
$uselist=~s/\s+/ /g;
print '<h3>Testing Use Commands:'.encode_entities($uselist)."</h3>\n";
+
print '<pre>'."\n";
for my $use (split(' ',$uselist)) { addin($use); }
print '</pre>'."\n";
print '<form method="POST">'."\n";
print '<table><tr>'."\n";
print '<td style="vertical-align:top;">Modules to test<br>CASE MATTERS
+!<br>Just like in perl</td>'."\n";
print '<td style="vertical-align:top;"> </td>'."\n";
print '<td style="vertical-align:top;"><textarea rows="10" cols="50"
+name="uselist">'.$uselist0.'</textarea></td>'."\n";
print '</tr><tr>'."\n";
print '<td style="vertical-align:top;"><input type="submit" value="Tes
+tIt"></td>'."\n";
print '</tr></table> </form>'."\n";
print '</body></html>';
exit;
sub addin {
my $mod=shift;
my $pre=shift;
my %inc0=%INC;
if ($pre) {print '<pre>'."\n";}
print encode_entities('new to %INC from adding use '.$mod.";\n");
eval 'use '.$mod.';' ;
print encode_entities("\n".'eval use ERROR:'.$@."\n") if ($@);
my %incnew=();
my $max=0; # this ends up as largest in %INC not largest that is new
+ , good nuf
for my $k (keys %INC) { $max=length($k) if (length($k)>$max);}
my $fmt=' %-'.$max.'s => %s'."\n";
for my $k (sort keys(%INC)) {
print encode_entities(sprintf($fmt,$k,$INC{$k})) unless ($inc0{$k
+});
}
print "\n";
if ($pre) {print '</pre>'."\n";}
}
| [reply] [d/l] |
|
|
Testing Use Commands:JSON::XS JSON::PP
new to %INC from adding use JSON::XS;
JSON/XS.pm => /usr/local/lib64/perl5/JSON/XS.pm
Types/Serialiser.pm => /usr/local/share/perl5/Types/Serialiser.pm
attributes.pm => /usr/share/perl5/attributes.pm
common/sense.pm => /usr/local/share/perl5/common/sense.pm
new to %INC from adding use JSON::PP;
B.pm => /usr/lib64/perl5/B.pm
Carp/Heavy.pm => /usr/share/perl5/Carp/Heavy.pm
JSON/PP.pm => /usr/local/share/perl5/JSON/PP.pm
List/Util.pm => /usr/lib64/perl5/List/Util.pm
Scalar/Util.pm => /usr/lib64/perl5/Scalar/Util.pm
bytes.pm => /usr/share/perl5/bytes.pm
| [reply] [d/l] |
|
|
Funny that it loads there but not with JSON, did you have a "use JSON::XS;" statment already in the program you ran the last time? It seems you have both of them.
kcott had some good debug tips at Re: pl script in webserver. Besides those there could be an error in your input to decode; you could try this.
eval {$respons = JSON::XS->new->decode ($res->content);} ;
print 'json decode error:'.$@ ."\n" if ($@);
Running the decode inside an eval will trap any "die", and $@ will contain the error you would have died with.
It is quite possible the error involves html encoding of data.
This may fix that.
eval {$respons = JSON::XS->new->decode ($res->decoded_content);} ;
print 'json decode error:'.$@ ."\n" if ($@);
it is quite possible that it is both html and UTF encoding problems. This would fix that
use Encode;
eval {$respons = JSON::XS->new->decode (Encode::decode_utf8($res->deco
+ded_content));};
print 'json decode error:'.$@ ."\n" if ($@);
If your web page came with proper utf settings, the content may already be marked with the utf flag and that may introduce its own error saying you cant decode data that is already marked utf. But i have gotten response content data that did not have the proper utf settings in the html before. This example came from code playing with the wordpress api.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
| [reply] |
|
|
In Soviet Russia, they used to shoot people for writing code like that:
print '<h3>Testing Use Commands:'.encode_entities($uselist)."</h3>\n";
+
print '<pre>'."\n";
for my $use (split(' ',$uselist)) { addin($use); }
print '</pre>'."\n";
print '<form method="POST">'."\n";
print '<table><tr>'."\n";
print '<td style="vertical-align:top;">Modules to test<br>CASE MATTERS
+!<br>Just like in perl</td>'."\n";
print '<td style="vertical-align:top;"> </td>'."\n";
print '<td style="vertical-align:top;"><textarea rows="10" cols="50"
+name="uselist">'.$uselist0.'</textarea></td>'."\n";
print '</tr><tr>'."\n";
print '<td style="vertical-align:top;"><input type="submit" value="Tes
+tIt"></td>'."\n";
print '</tr></table> </form>'."\n";
print '</body></html>';
Use a templating solution. At least just use one print and provide the strings as a list. | [reply] [d/l] |
Re: pl script in webserver
by marto (Cardinal) on Apr 08, 2017 at 18:47 UTC
|
Ensure the modules you use are installed on your host. Check the server error logs to get the actual error messages.
| [reply] |
Re: pl script in webserver
by 1nickt (Canon) on Apr 08, 2017 at 19:57 UTC
|
Most likely you are right and your web host does not provide JSON::XS. If so, you can try using JSON::PP instead. If your Perl is v5.14 or newer, JSON::PP is in the core distribution. If your Perl is older than that you can simply copy the source code to some place in your home directory and load it from there.
If you don't know how to find out what version of Perl you are using, do some searches, it's easy enough.
Hope this helps!
The way forward always starts with a minimal test.
| [reply] |
|
|
both modules are installed, but what i have discovered.
when its like this it cause the problem am facing
my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, );
but when its like this
my $ua = new LWP::UserAgent(keep_alive=>1);
i get this malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "<html>\r\n<head><tit...")
same when its like this LWP::UserAgent->new;
| [reply] [d/l] [select] |
Re: pl script in webserver
by huck (Prior) on Apr 08, 2017 at 20:00 UTC
|
#!/usr/bin/perl
use strict; use warnings;
print "content-type: text/plain\n\nTesting Use commands\n\n";
#addin('Data::Dumper');
#addin('strict');addin('warnings');
#addin('Config');addin('Fcntl');
#addin('CGI::Carp qw(fatalsToBrowser)');
#addin('GD');
#addin('LWP::UserAgent');
addin('JSON');
addin('x::x');
sub addin {
my $mod=shift;
my $pre=shift;
my %inc0=%INC;
if ($pre) {print '<pre>'."\n";}
print "\n";
print 'new to %INC from adding use '.$mod."\n";
eval 'use '.$mod.';' ;
print 'eval use ERROR:'.$@."\n" if ($@);
my %incnew=();
my $max=0; # this ends up as largest in %INC not largest that is new
+ , good nuf
for my $k (keys %INC) { $max=length($k) if (length($k)>$max);}
my $fmt=' %-'.$max.'s => %s'."\n";
for my $k (sort keys(%INC)) {
printf $fmt,$k,$INC{$k} unless ($inc0{$k});
}
if ($pre) {print '</pre>'."\n";}
}
It is customized for your JSON instance, but you can put in any lines to load anymodule you like, as the comment out lines show.
It will either display what gets added to your program when a use statment is executed, or an error message like my example of x::x shows.
result for my server
Testing Use commands
new to %INC from adding use JSON
Carp.pm => /usr/share/perl/5.18/Carp.pm
Exporter.pm => /usr/share/perl/5.18/Exporter.pm
Exporter/Heavy.pm => /usr/share/perl/5.18/Exporter/Heavy.pm
JSON.pm => /usr/share/perl5/JSON.pm
JSON/XS.pm => /usr/lib/perl5/JSON/XS.pm
XSLoader.pm => /usr/share/perl/5.18/XSLoader.pm
attributes.pm => /usr/lib/perl/5.18/attributes.pm
base.pm => /usr/share/perl/5.18/base.pm
common/sense.pm => /usr/lib/perl5/common/sense.pm
constant.pm => /usr/share/perl/5.18/constant.pm
overload.pm => /usr/share/perl/5.18/overload.pm
overloading.pm => /usr/share/perl/5.18/overloading.pm
vars.pm => /usr/share/perl/5.18/vars.pm
warnings/register.pm => /usr/share/perl/5.18/warnings/register.pm
new to %INC from adding use x::x
eval use ERROR:Can't locate x/x.pm in @INC (you may need to install th
+e x::x module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /
+usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/
+perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at (eval 8
+) line 1.
BEGIN failed--compilation aborted at (eval 8) line 1.
Hope it helps.
| [reply] [d/l] [select] |
|
|
new to %INC from adding use Data::Dumper
Data/Dumper.pm => /usr/lib64/perl5/Data/Dumper.pm
bytes.pm => /usr/share/perl5/bytes.pm
new to %INC from adding use strict
new to %INC from adding use warnings
new to %INC from adding use Config
new to %INC from adding use Fcntl
new to %INC from adding use CGI::Carp qw(fatalsToBrowser)
CGI/Carp.pm => /usr/share/perl5/CGI/Carp.pm
File/Spec.pm => /usr/lib64/perl5/File/Spec.pm
File/Spec/Unix.pm => /usr/lib64/perl5/File/Spec/Unix.pm
new to %INC from adding use GD
FileHandle.pm => /usr/share/perl5/FileHandle.pm
GD.pm => /usr/local/lib64/perl5/GD.pm
GD/Image.pm => /usr/local/lib64/perl5/GD/Image.pm
GD/Polygon.pm => /usr/local/lib64/perl5/GD/Polygon.pm
IO/File.pm => /usr/lib64/perl5/IO/File.pm
IO/Seekable.pm => /usr/lib64/perl5/IO/Seekable.pm
new to %INC from adding use LWP::UserAgent
new to %INC from adding use JSON
JSON.pm => /usr/local/share/perl5/JSON.pm
new to %INC from adding use x::x
eval use ERROR:Can't locate x/x.pm in @INC (@INC contains:
/home/breflf/perl5/lib/perl5
/home/breflf/perl5/lib/perl5/x86_64-linux-thread-multi
/home/breflf/perl/usr/local/lib64/perl5
/home/breflf/perl/usr/local/share/perl5
/home/breflf/perl/usr/lib64/perl5/vendor_perl
/home/breflf/perl/usr/share/perl5/vendor_perl
/home/breflf/perl/usr/lib64/perl5
/home/breflf/perl/usr/share/perl5
/usr/local/lib64/perl5 /usr/local/share/perl5
/usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl
/usr/lib64/perl5 /usr/share/perl5 .) at (eval 25) line 1.
BEGIN failed--compilation aborted at (eval 25) line 1.
Carp/Heavy.pm => /usr/share/perl5/Carp/Heavy.pm
| [reply] [d/l] |
|
|
The failure on x::x was just to show you how a failure would printout. You can ignore it.
JSON loaded fine for you. On mine loading JSON also loads JSON::XS. Yours didnt.
Try the one below at Re: pl script in webserver and enter JSON::XS into the box. Add JSON::PP too. I bet it shows a failure on JSON::XS, that means you dont have JSON::XS. If it doesnt show a failure on JSON:PP use that instead.
Edit to add: Interesting that LWP::UserAgent didnt fail, but didnt add anything either, as if it was already loaded for some reason.
| [reply] |
|
|
Re: pl script in webserver
by poj (Abbot) on Apr 08, 2017 at 20:08 UTC
|
| [reply] |