I am getting this strange error when importing a module I wrote into my Dancer app.
Prototype mismatch: sub main::from_json: none vs ($@) at mymodule.pm line 6.
Prototype mismatch: sub main::to_json: none vs ($@) at mymodule.pm line 6.
I guess this is because in my module I'm importing the perl JSON module.
Everything seems to perform fine, but I'm wondering what this error/warning is all about? I can't seem to find anything about it online.
Another situation where this arises is when some other module you have loaded defines a from_json/to_json. An example I've hit a couple times recently is with Dancer. If you have a package with
package Foo;
use Dancer qw/:syntax/;
use JSON;
1;
You will get that warning because (apparently) Dancer with the :syntax import puts a from_json and to_json into your namespace.
A quick solution in this situation is to just explicitly import nothing from JSON:
package Foo;
use Dancer qw/:syntax/;
use JSON qw//;
1;
Then in your code you will need to use the full package name to get JSON's subs, like this:
my $hash = JSON::from_json('{"bob":"sally"}');
In a situation like this, though, you want to use the full package names so it's clear which function you're getting--there are multiple declarations of to_json/from_json, so let's be very clear which one we mean.
If you put the following in Foo.pm and run with "perl Foo.pm", with and without the qw//
after the use JSON
, you can see how it works:
package Foo;
use Dancer qw/:syntax/;
use JSON qw//;
print Dumper( JSON::from_json('{"bob":"sally"}') ); use Data::Dumper;
1;
I believe Dancer/2 provides to_json
and from_json
to you, so you don't have to use JSON.
This will work:
use Dancer2 ':syntax';
get '/cheeseburgers' => {
return to_json($restaurant->make_cheeseburgers);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With