开发者

how to fake a perl module for dependency?

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜