Json in Perl error in opensips - json

What is the difference between json and json::PP in Perl?
I meet this error when use Json and Json:PP when writing perl script in opensips
ERROR:core:XS_OpenSIPS__Message_log:
perl warning: Prototype mismatch: sub main::decode_json ($) vs none.
I have problem with these codes:
my %postObject = ("callId" => $callID);
$postObject{'endTime'} = time() . "";
$postObject{'key'} = "12345#qwerty";
my $post_data = encode_json \%postObject;

The "Prototype mismatch" warning typically means that you've defined a sub twice in some way, and the two definitions' prototypes don't match.
Do you have a sub decode_json ($) in your main code somewhere? If you do, I'd suggest removing or renaming it, because it is conflicting with decode_json from one of the JSON modules. If you don't, then you may be getting a second decode_json from another module you are loading, in which case you'd have to track that down, or provide us with a Minimal, Complete, and Verifiable example.
I'd strongly recommend turning on warnings, because then you will additionally get "Subroutine redefined" warnings to help you track the issue down.

Related

Extracting values from hash created by perl JSON::Syck::Load

I've got a very simple Perl issue that I can't for the life of me figure out.
I'm consuming JSON formatted data from a REST endpoint in a perl script. The data is shaped like this:
{
"ScriptRunning": true
}
There's some other stuff, but really all I care about is the ScriptRunning tag. I'm consuming this data using JSON::Syck::Load like so:
my $running_scripts = JSON::Syck::Load($output_from_rest_call)
(in my current environment it is not possible to get other libraries for CPAN, so I'm stuck with that). All that is working correctly as far as I can tell, I used Data::Dumper to confirm the data looks good:
$VAR1 = {
'ScriptRunning' => 1 # or '' if false
}
However, I can't figure out how to actually get the value of 'ScriptRunning'. I've done print ref $running_scripts and confirmed that it is a HASH, however when I try to index into the hash I'm not getting anything. I've tried the following:
my $script_is_running = $running_scripts{'ScriptRunning'};
my $script_is_running = $running_scripts{ScriptRunning};
my $keys_in_running_scripts = keys $running_scripts; # keys_in_running_scripts is empty
my $keys_in_running_scripts = keys %running_scripts; # keys_in_running_scripts is empty
Any ideas?
You need to use strict; (and use warnings; while you are at it, maybe use diagnostics; too, when you are really stuck). As a general rule, ALWAYS use strict; and use warnings; because they prevent problematic code from running and give you some much more helpful output.
You should also read perldoc perlreftut, which helps explain what you are dealing with.
Your variable $running_scripts is not a hash, but a "hash reference", which is an important distinction. When you call ref on a real hash, it returns a false value, since it is not a reference.
What you need to do is "dereference" using the arrow operator to get the value.
To get the keys call to work, there's a separate syntax for dereferencing.
my $script_is_running = $running_scripts->{ScriptRunning};
my #keys_in_running_scripts = keys %{$running_scripts};

How do I debug lua functions called from conky?

I'm trying to add some lua functionality to my existing conky setup so that repetitive "code" in my conky text can be cleaned up. For example, I have information for each mounted FS, each core, etc. where each row displayed in my panel differs ONLY by one parameter.
My first skeletal, attempt at using lua functions for this seems to run but displays nothing in my panel. I've only found very simple examples to base this on, so I may have made a simple error, but I don't even know how to diagnose it. My code here is modeled after what I HAVE been able to find regarding writing functions, such as this How to implement a basic Lua function in Conky? , but that's about all the depth I've found on the topic except for drawing and cairo examples.
Here's the code added to my conky config, as well as the contents of my functions.lua file
conky.config = {
...
lua_load = '/home/conky-manager/MyConky/functions.lua',
};
conky.text = [[
...
${voffset 5}${lua conky_test 'test'}
...
]]
file - functions.lua
function conky_test(parm1)
return 'result text'
end
What I would expect is to see is "result text" displayed in my panel at the location where that function call appears, but nothing shows.
Is there a log created by conky as it runs, or a way to provide some debug output? Even if I'd made a simple error here, I'd still like to have the ability to diagnose things as my code gets more complex.
Success!
After cobbling info from several articles together, I figured out my basic flaws -
1. Missing a 'conky_main' function,
2. Missing a 'lua_draw_hook_post' to invoke it, and
3. Realizing that if I invoke conky from a terminal, print statements in lua would appear there.
So, for anyone who sees this question and has the same issues, here's the corrected code.
conky.config = {
...
lua_load = '/home/conky-manager/MyConky/functions.lua',
lua_draw_hook_post = "main",
};
conky.text = [[
...
${lua conky_test 'test'}
...
]]
and the proper basics in my functions.lua file
function conky_test(parm1)
return 'result text'
end
function conky_main()
if conky_window == nil then
return
end
end
A few notes:
I still haven't determined if using 'lua_draw_hook_pre' instead of 'lua_draw_hook_post' makes any difference, but it doesn't seem to in this example.
Also, some examples showed actually calling this 'test' function instead of writing a 'main', but the 'main' seemed to have value in checking to see if conky_window existed.
Some examples seemed to state that naming functions with the prefix 'conky_' was required, but then showed examples of calling those functions without the prefix, so I assume the prefix is inferred during the call.
a major note: you should run conky from the directory containing the lua scripts.

Perl: JSON fails if a thread is started

Can someone please tell my why JSON is not working if some thread is started?
use strict;
use warnings;
use JSON;
use threads;
use threads::shared;
sub th { }
threads->create(\&th)->join() if $ARGV[0];
my $json = to_json({ val => "123"}); # WTF?!?
print "$json\n";
Works fine and prints the JSON-string. But pass 1 as an argument to the script to create the thread and to_json will fail with
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)
Same effect if I use encode_json insead.
On the manpage of JSON the word thread is not present and I see no reason why a thread should harm an outside string-conversion.
???
JSON(.pm) is just a front end for JSON::PP, JSON::XS or Cpanel::JSON::XS.
You have found a bug in JSON::XS. About this, JSON::XS's documentation says:
(I-)THREADS
This module is not guaranteed to be ithread (or MULTIPLICITY-) safe and there are no plans to change this. Note that perl's builtin so-called theeads/ithreads are officially deprecated and should not be used.
[Note that the last part is incorrect. The official position is actually: Threads are hard, so you should use something else instead. It's highly questionable since the alternatives are arguably just as hard.]
Workaround: Use one of the other backends (directly or via JSON(.pm)).
$ PERL_JSON_BACKEND=JSON::XS 46793885 0
{"val":"123"}
$ PERL_JSON_BACKEND=JSON::XS 46793885 1
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at /home/ikegami/usr/perlbrew/perls/5.26.0t/lib/site_perl/5.26.0/JSON.pm line 170.
$ PERL_JSON_BACKEND=Cpanel::JSON::XS 46793885 1
{"val":"123"}
$ PERL_JSON_BACKEND=JSON::PP 46793885 1
{"val":"123"}
You can control this within the script by adding the following before loading JSON:
BEGIN { $ENV{PERL_JSON_BACKEND} = 'Cpanel::JSON::XS' }
I ran into this as well (trying to use JSON with multi-threaded perl). Without launching a background thread, my code worked fine, but got the same error you are getting when there was a thread launched.
Like you, I didn't find any help online specific to threading with regards to this error text. However, following the allow_nonref error text, I found the following in JSON::XS's documentation:
"OLD" VS. "NEW" JSON (RFC 4627 VS. RFC 7159)
TL;DR: Due to security concerns, JSON::XS will not allow scalar data in JSON >texts by default - you need to create your own JSON::XS object and enable
allow_nonref:
my $json = JSON::XS->new->allow_nonref;
$text = $json->encode ($data);
$data = $json->decode ($text);
In your case, you are trying to call to_json, which internally creates a JSON object and calls encode on it. Unfortunately it doesn't specify allow_nonref in its constructor. So to make your code work, you can do something like this:
use JSON::XS;
my $json_obj = JSON::XS->new->allow_nonref;
my $json = $json_obj->encode({ val => "123"});
print "$json\n";
I came up with this solution before reading the other responses here, so those may be better solutions, but this should at least get you past the issue with minimal changes.
This is definitely to do with JSON and global state.
If you require and import JSON, after the thread invocation, it 'works'.
The warning in the module for JSON::XS includes:
(I-)THREADS ^
This module is not guaranteed to be ithread (or MULTIPLICITY-) safe and there are no plans to change this
The 'workaround' for a not-thread-safe module is to not load it via use (which happens at 'compile' time) and instead require and import (at runtime) after the parallel instances of the program (threads) have been started.
E.g.:
use strict;
use warnings;
use threads;
use threads::shared;
sub th { }
my $th = threads->create( \&th )->join;
## Just inside main thread
##can do this within individual threads instead if desired
require JSON;
JSON->import;
my $json = to_json({ val => "123" }); # WTF?!?
print "\n$json\n";

passing variables to perl script from html

I am trying to call a perl script from my HTML page. The way am trying to do is to call the url of the perl script located on the server.
Here is the piece of code:
HTML:
var fname = "Bob";
var url='http://xxx.com:30000/cgi-bin/abc.pl?title=fname';
window.open(url,"_self");
The way am trying to retrieve it in perl as:
Perl:
print "$ARGV[0]\n";
Now, I have 3 questions:
I think this is the correct way to pass the variables but am not able to print the argument in perl.
If i want to pass another variable lname, how do i append it to the url?
My window.open should open the output in the same window, since it uses the parameter _self. Still it doesn't.
Could anybody point out the problems?
Thanks,
Buzz
No #ARGV contains command line arguments and will be empty.
You need the CGI module
use warnings;
use strict;
use CGI;
my $query = CGI->new;
print $query->param( 'title' );
Edit:
Take a look at dan1111's answer on how to generate HTML and display it in the browser.
In addition to what Matteo said, a simple print statement is not enough to send some output to the browser.
Please see a recent answer I wrote giving a sample CGI script with output.
In regard to your other issues:
Variables are appended to a url separated with &:
var url='http://xxx.com:30000/cgi-bin/abc.pl?title=fname&description=blah';
Based on this question, perhaps you should try window.location.href = url; instead (though that doesn't explain why your code isn't working).
There are two different environments that each pass variables two different ways. The command line can pass variables through the #ARGV and the browser can pass variables through #ENV. It doesn't matter what language you use, those are the arrays that you will have to employ.

Why does XML::LibXML keeps printing errors even when I disable them?

I'm using XML::LibXML to parse a document.
The HTML file behind it, has some minor errors, and the parser reports them:
http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined
nal URL was http://google.com<span id="smallink"
^
http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined
and use http://is.gd/fNqtL-<span id="smallink"
^
However, I disabled error reporting:
my $parser = XML::LibXML->new();
$parser->set_options({ recover => 2,
validation => 0,
suppress_errors => 1,
suppress_warnings => 1,
pedantic_parser => 0,
load_ext_dtd => 0, });
my $doc = $parser->parse_html_file("http://is.gd/create.php?longurl=$url");
My only option to suppress those errors, is to run the script with 2>/dev/null, which I don't want. Could someone help me please get rid of those errors?
I have no idea if you're asking XML::LibXML corretly to not print its warnings. I'll assume you are and this is a bug in XML::LibXML (which you should also report to the author), and only address how to suppress warnings.
Every time a warning is about to be printed, perl will look up the value of $SIG{__WARN__} and, if that contains a code reference, invoke it instead of printing the warning itself.
You can use that stop the warnings you want to ignore to be printed to STDERR. However, you should be careful with this. Make sure to only suppress false-positives, not all warnings. Warnings are usually useful. Also, make sure to localize your use of $SIG{__WARN__} to the smallest possible scope to avoid odd side effects.
# warnings happen just as always
my $parser = ...;
$parser->set_options(...);
{ # in this scope we filter some warnings
local $SIG{__WARN__} = sub {
my ($warning) = #_;
print STDERR $warning if $warning !~ /validity error/;
};
$parser->parse_html_file(...);
}
# more code, now the warnings are back to normal again
Also note that this is all assuming those warnings come from perl-space. It's quite possible that libxml2, the C library XML::LibXML uses under the hood, writes warnings directly to stderr itself. $SIG{__WARN__} will not be able to prevent it from doing that.
A possible solution is to install a $SIG{__WARN__} handler which filters the messages or just silences all warnings:
local $SIG{__WARN__} = sub { /* $_[0] is the message */ };