如何伪造 Perl 模块以获取依赖?

发布于 2024-11-17 10:08:11 字数 149 浏览 3 评论 0原文

我正在使用的外部 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 技术交流群。

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

发布评论

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

评论(1

故人的歌 2024-11-24 10:08:11

所以我认为这里没有几个问题。

当您说依赖时,您的意思是外部模块只是尝试 requireuse DBD::mysql?如果是这种情况,那么您应该建议开发人员不要明确这样做,因为这违背了使用 DBI。应根据 DSN 即时选择数据库驱动程序。

假设作者只是使用包名称,因为他认为这是一件有用或有意义的事情,那么是的,您可以覆盖该包,并且有有几种方法可以做到这一点。

正如您所建议的,您只需创建自己的模块 DBD/mysql.pm 来定义 DBD::mysql 包。

如果您有兴趣,还可以做一些其他事情。您只需让 Perl 相信该模块已加载,而不是在源代码树中散布假目录和文件。我们可以通过直接操作%INC来做到这一点。

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

只需添加此哈希键,我们就可以阻止在文件系统中搜索有问题的模块。请注意,这是在 BEGIN 块中。如果外部作者执行了 use,那么我们必须在评估 use 语句之前填充此值。 use 语句相当于 requireimport 包含在 BEGIN 中。

现在让我们从一般意义上进一步推测外部作者试图调用包的方法。如果这些符号不存在,您将收到运行时错误。您可以利用 Perl 的 AUTOLOAD 来拦截此类调用并执行正确的操作。 正确的事情可能有很大差异,从简单地记录消息到更复杂的事情。例如,您可以使用此工具通过监视所有调用来检查作者引入的耦合深度。

package DBD::mysql;

sub AUTOLOAD {
    printf(
        "I don't wanna '%s' called from '%s'\n", $AUTOLOAD, caller(0)
      );
}

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

DBD::mysql::blah()

现在我们还讨论一下这样的情况:违规作者还创建了一些面向对象的类实例,并且他的代码没有正确考虑
为你的存根代码。我们将存根我们假设是 new 的构造函数,以使用我们的包名称来祝福匿名哈希。这样你就不会得到
当他调用实例上的方法时出错。

package DBD::mysql;

sub AUTOLOAD {
    printf(
        "I don't wanna '%s' called from '%s'\n", $AUTOLOAD, caller(0)
    );
}

sub new {
    bless({}, __PACKAGE__)
}

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

my $thing = new DBD::mysql;

$thing->blah()

So I think there are few issues here.

When you say dependency, do you mean the external module simply tries to require or use 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 using DBI. The database driver should be selected on the fly based on the DSN.

Assuming that the author is merely useing 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 the DBD::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.

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

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 a use then we must populate this value before the use statement is evaluated. The use statements are equivalent to a require and import wrapped in a BEGIN.

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.

package DBD::mysql;

sub AUTOLOAD {
    printf(
        "I don't wanna '%s' called from '%s'\n", $AUTOLOAD, caller(0)
      );
}

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

DBD::mysql::blah()

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 get
errors when he calls methods on an instance.

package DBD::mysql;

sub AUTOLOAD {
    printf(
        "I don't wanna '%s' called from '%s'\n", $AUTOLOAD, caller(0)
    );
}

sub new {
    bless({}, __PACKAGE__)
}

package main;    # or whereever

BEGIN {
    $INC{'DBD/mysql.pm'} = "nothing to see here";
}

my $thing = new DBD::mysql;

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