Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype mismatch error (perl)

Tags:

module

perl

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.

like image 495
pepper Avatar asked Apr 02 '13 17:04

pepper


2 Answers

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;
like image 142
msouth Avatar answered Sep 18 '22 18:09

msouth


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);
}
like image 40
a7omiton Avatar answered Sep 18 '22 18:09

a7omiton