Perl函数名称冲突 [英] Perl function name clash

查看:141
本文介绍了Perl函数名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于一个我正在使用的模块中有一个名称与我自己模块中的名称完全相同的函数。当我尝试在我的模块中调用函数时(OO Perl,所以 $ self->函数),它会调用另一个模块的函数。



我已经通过重命名我的函数来解决它了,但作为一个感兴趣的问题,有什么方法可以从我的模块明确调用函数吗?

编辑:
这实际上就是我在做的事情

  package Provider :: WTO; 

使用base qw(Provider); #提供程序包含一个名为date

使用utilities :: utils的方法; #不是我的模块,所以不要怪我这个可怕的名字:-)
...
sub _get_location
{
my $ self = shift;
返回$ self->日期。/ some_other_string; #calls utilities :: utils :: date()
}


解决方案如果名称冲突是由另一个模块导入导致的,则可以考虑 Sub :: Import ,这可以轻松重命名导入,即使导出模块没有明确地支持它,或者 namespace :: autoclean / namespace :: clean

  package YourPackage; 

使用Sub :: Import'Some :: Module'=> (
foo => {-as =>'moo'},
); #import foo as moo

sub foo {#your own foo()
return moo()* 2; #call Some :: Module :: foo()as moo()
}

命名空间清理模块只有在导入用函数映射任何方法时才会有帮助,而不是在任何其他情况下:

 打包YourPackage; 

使用Some :: Module; #import foo
使用Method :: Signatures :: Simple
使用namespace :: autoclean; #或使用namespace :: clean -except => 元;

方法foo {
return foo()* 2; #调用导入的东西作为函数
}

方法栏{
return $ self-> foo; #作为方法调用自己的foo()
}

1;

这样,当函数调用foo()时,导入的函数将在编译模块后被删除。已经受到进口的约束。稍后,在您的模块运行时,将会安装一个名为 foo 的方法。方法解析总是在运行时发生,所以对 - > foo的任何方法调用都将被解析为你自己的方法。

另外,你可以总是调用一个函数,并且不要导入它。

  use Some :: Module(); 
Some :: Module :: foo();

这也可以用于方法,完全禁用运行时方法查找:

  $ obj-> Some :: Module :: foo(); 

然而,需要这样做通常是设计不好的标志,您应该稍微退一步并解释你为了让你进入这种状况所做的一切。


I'm in a situation where a module I'm using has a function whose name is exactly the same as one in my own module. When I try to call the function in my module (OO Perl, so $self->function) it's calling the function from the other module instead.

I've already got around it by renaming my function but as a matter of interest, is there any way of explicitly calling the function from my module?

edit: this is essentially what I'm doing

package Provider::WTO;

use base qw(Provider); # Provider contains a method called date

use utilities::utils; #not my module so don't blame me for the horrendous name :-)
...
sub _get_location
{
    my $self = shift;
    return $self->date."/some_other_string"; # calls utilities::utils::date()
}

解决方案

If the name conflict is caused by an import from another module you might consider either Sub::Import, which allows for easy renaming of imports, even if the exporting module doesn't explicitly support that, or namespace::autoclean/namespace::clean.

package YourPackage;

use Sub::Import 'Some::Module' => (
    foo => { -as => 'moo' },
); # imports foo as moo

sub foo { # your own foo()
    return moo() * 2; # call Some::Module::foo() as moo()
}

The namespace cleaning modules will only be helpful if the import is shadowing any of your methods with a function, not in any other case:

package YourPackage;

use Some::Module; # imports foo
use Method::Signatures::Simple
use namespace::autoclean; # or use namespace::clean -except => 'meta';

method foo {
    return foo() * 2; # call imported thing as a function
}

method bar {
    return $self->foo; # call own foo() as a method
}

1;

This way the imported function will be removed after compiling your module, when the function calls to foo() are already bound to the import. Later, at your modules runtime, a method called foo will be installed instead. Method resolution always happens at runtime, so any method calls to ->foo will be resolved to your own method.

Alternatively, you can always call a function by it's fully-qualified name, and don't import it.

use Some::Module ();
Some::Module::foo();

This can also be done for methods, completely disabling runtime method lookup:

$obj->Some::Module::foo();

However, needing to do this is usually a sign of bad design and you should probably step back a little and explain what you did to get you into this situation in the first place.

这篇关于Perl函数名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆