如何伪造 Perl 模块以获取依赖?
我正在使用的外部 Perl 库有一个依赖项 (DBD::mysql),我不会在我的应用程序 (DBD::SQLite) 中使用它,因此我希望系统假装依赖项存在,即使它存在一个“假”。
我可以创建一个空的 DBD::mysql.pm 模块来编译吗?或者有更直接的方法吗?
An external Perl library that I am using has a dependency (DBD::mysql) that I will not be using in my application (DBD::SQLite), so I would like the system to just pretend the dependency is there, even if it's a "fake".
Can I just create an empty DBD::mysql.pm module that compiles or is there a more straightforward way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我认为这里没有几个问题。
当您说依赖时,您的意思是外部模块只是尝试
require
或use
DBD::mysql
?如果是这种情况,那么您应该建议开发人员不要明确这样做,因为这违背了使用DBI
。应根据 DSN 即时选择数据库驱动程序。假设作者只是
使用
包名称,因为他认为这是一件有用或有意义的事情,那么是的,您可以覆盖该包,并且有有几种方法可以做到这一点。正如您所建议的,您只需创建自己的模块
DBD/mysql.pm
来定义DBD::mysql
包。如果您有兴趣,还可以做一些其他事情。您只需让 Perl 相信该模块已加载,而不是在源代码树中散布假目录和文件。我们可以通过直接操作
%INC
来做到这一点。只需添加此哈希键,我们就可以阻止在文件系统中搜索有问题的模块。请注意,这是在
BEGIN
块中。如果外部作者执行了use
,那么我们必须在评估use
语句之前填充此值。use
语句相当于require
和import
包含在BEGIN
中。现在让我们从一般意义上进一步推测外部作者试图调用包的方法。如果这些符号不存在,您将收到运行时错误。您可以利用 Perl 的 AUTOLOAD 来拦截此类调用并执行正确的操作。 正确的事情可能有很大差异,从简单地记录消息到更复杂的事情。例如,您可以使用此工具通过监视所有调用来检查作者引入的耦合深度。
现在我们还讨论一下这样的情况:违规作者还创建了一些面向对象的类实例,并且他的代码没有正确考虑
为你的存根代码。我们将存根我们假设是 new 的构造函数,以使用我们的包名称来祝福匿名哈希。这样你就不会得到
当他调用实例上的方法时出错。
So I think there are few issues here.
When you say dependency, do you mean the external module simply tries to
require
oruse
DBD::mysql
? If that is the case then you should advise the developer that he shouldn't be explicitly doing that because that defeats the purpose of usingDBI
. The database driver should be selected on the fly based on the DSN.Assuming that the author is merely
use
ing the package name because he thought that was a useful or meaningful thing to do, then yes, you may override that package, and there are a few ways to do it.As you suggested, you can merely create your own module
DBD/mysql.pm
that would define theDBD::mysql
package.There are some other things you could do if you are interested. Instead of littering your source tree with fake directories and files, you just need to convince Perl that the module was loaded. We can do this by directly manipulating
%INC
.Simply by adding this hash key, we preclude a search of the filesystem for the offending module. Observe that this is in a
BEGIN
block. If the external author did ause
then we must populate this value before theuse
statement is evaluated. Theuse
statements are equivalent to arequire
andimport
wrapped in aBEGIN
.Now lets further speculate in the general sense that the external author was attempting to call methods of the package. You will get run time errors if there if those symbols don't exist. You can take advantage of Perl's
AUTOLOAD
to intercept such calls and do the right thing. What The right thing is can vary a lot, from simply logging a message to something more elaborate. For instance, you could use this facility to examine the depth of the coupling that the author introduced by monitoring all the calls.Now let's also cover the case where the offending author also created some object oriented instances of a class, and his code doesn't properly account
for your stub code. We will stub the constructor which we assume is
new
to just bless an anonymous hash with our package name. That way you won't geterrors when he calls methods on an instance.