我想知道是否有可能确保我制作的类中的方法不会被猴子修补(Monkey patch)。穆斯能做到这一点吗?
考虑以下:
{
package Foo;
sub hello{print "HI"}
1;
}
package main;
sub Foo::hello {print "bye"}
Foo::hello()#bye
最佳答案
经过快速的网络研究,我在Perlmonks上发现了this线程,该线程指出:
As for declaring methods final, I'm not sure how you would do it without doing something really fancy to intercept all additions to the symbol table. (Can that even be done?).
我还认为这是不可能的。
使用Moose可以应用Method Modifiers,它允许您定义必须在调用函数之前运行的函数。
我没有尝试过,但是也许你可以定义一个函数
before "hello" => sub{ # check if hello has been tampered with
}
我不知道该如何检查它甚至不起作用,但似乎值得尝试!但是我要补充一点,由于perl是一种解释语言,任何使用您的软件包的人都可以查看和编辑源代码,因此可以避免任何预防措施。
关于perl - 如何在Perl中制作 “final”方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9415347/