原型不匹配错误(Perl) [英] Prototype mismatch error (perl)
问题描述
将我编写的模块导入我的Dancer应用程序时,我得到了这个奇怪的错误.
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.
我猜这是因为在我的模块中,我正在导入perl JSON模块.
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.
推荐答案
发生这种情况的另一种情况是,当您已加载的其他模块定义了from_json/to_json时.我最近打过几次的例子是《舞者》.如果您的包裹带有
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;
您会收到该警告,因为(显然)具有:syntax导入功能的Dancer会将from_json和to_json放入您的命名空间.
You will get that warning because (apparently) Dancer with the :syntax import puts a from_json and to_json into your namespace.
在这种情况下的快速解决方案是仅从JSON显式导入任何内容:
A quick solution in this situation is to just explicitly import nothing from JSON:
package Foo;
use Dancer qw/:syntax/;
use JSON qw//;
1;
然后在代码中,您将需要使用完整的包名称来获取JSON的子项,如下所示:
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"}');
在这种情况下,您要使用完整的程序包名称,这样就很清楚您要获得哪个功能-to_json/from_json有多个声明,因此让我们非常清楚我们的意思.
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.
如果将以下内容放在Foo.pm中,并使用"perl Foo.pm"运行,并且在use JSON
之后带有和不带有qw//
,则可以看到它的工作原理:
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;
这篇关于原型不匹配错误(Perl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!