Prototype mismatch error (perl)

10,603

Solution 1

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;

Solution 2

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

Solution 3

The reason I was getting this error was because in my own module, I was using the use directive and importing JSON and other modules BEFORE I declared my own package namespace, with

package mymodule

instead of AFTER. The package declaration has to come first.

Solution 4

See Prototypes in perlsub. The functions from_json and to_json were defined with different prototypes than used in the code.

Share:
10,603

Related videos on Youtube

pepper
Author by

pepper

Updated on September 14, 2022

Comments

  • pepper
    pepper over 1 year

    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.

    • TLP
      TLP about 11 years
      Do you by "importing the JSON module" mean "pre-declaring the subroutines from_json and to_json"? With something like sub from_json;?
    • pepper
      pepper about 11 years
      Don't really understand why this question has been downvoted. Seems perfectly reasonable to me.
    • Miguel Prz
      Miguel Prz about 11 years
      did you use from_json and to_json passing 1 or 2 scalar arguments?
  • pepper
    pepper about 9 years
    very true. it's included into the package, not only dancer2 but dancer (the original version) as well. Still, I was using perl packages incorrectly, and this error was more of a general one, pertaining to the order in which "use" and "require" should appear on a script.