Perl 函数名称冲突

发布于 2024-09-18 08:12:02 字数 529 浏览 6 评论 0原文

我遇到的情况是,我正在使用的模块有一个函数,其名称与我自己的模块中的函数完全相同。当我尝试调用模块中的函数(OO Perl,因此 $self->function)时,它会从其他模块调用该函数。

我已经通过重命名我的函数来解决这个问题,但有趣的是,有什么方法可以从我的模块中显式调用该函数吗?

编辑: 这本质上就是我正在做的事情

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

双马尾 2024-09-25 08:12:02

如果名称冲突是由另一个模块的导入引起的,您可以考虑使用 Sub::Import,它允许轻松重命名导入,即使导出模块没有明确支持该操作,或者 <代码>命名空间::autoclean/命名空间::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()
}

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

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;

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

或者,您始终可以通过函数的完全限定名称来调用函数,而不导入它。

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

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

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

但是,需要这样做通常是设计不良的标志,您可能应该退后一步,解释一下您首先做了什么才让您陷入这种情况。

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.

奢欲 2024-09-25 08:12:02

您是否需要有问题的模块中的子例程?在不了解更多信息的情况下,我认为快速解决方法是明确不使用空导入列表导入它:

 use Interfering::Module ();

如果您需要其他导入的内容,您可以指定您需要的内容:

 use Interfering::Module qw(sub1 sub2);

如果您想要的导出列表非常长,您可以排除干扰子例程:

 use Interfering::Module qw(!bad_sub);

如果这些都不起作用,您将不得不更多地了解干扰模块。

Do you need that subroutine from the offending module? Without knowing more about it, I think the quick fix is to explicitly not import it with an empty import list:

 use Interfering::Module ();

If you need other imported things, you can specify the ones that you need:

 use Interfering::Module qw(sub1 sub2);

If the list of exports you want is really long, you can just exclude the interfering subroutine:

 use Interfering::Module qw(!bad_sub);

If none of those work, you'll have to say more about the interfering module.

叹沉浮 2024-09-25 08:12:02

您确定这是在方法调用(即 $self->function)中发生的而不是常规调用吗?

如果是这种情况,那么我能看到发生这种情况的唯一方法是您的模块正在扩展另一个模块,并且您没有定义有问题的方法,并且您正在扩展的模块定义了一个与您尝试调用的方法同名的函数。

无论如何,您都无法使用 useforeign::Module () 将有问题的函数导入到您的命名空间中。

如果这是一个被破坏的常规函数​​调用,您可以将其称为 Your::Module->function

Are you sure this is happening in a method call (i.e. $self->function) and not a regular call?

If that's the case, then the only way I can see of that happening is your module is extending the other module, and you're not defining the method in question, and the module you're extending defines a function with the same name as the method you're trying to call.

In any case, you can not import the offending function into your namespace with use Foreign::Module ().

If it's a regular function call that's getting clobbered, you can refer to it as Your::Module->function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文