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
requireoruseDBD::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
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.pmthat would define theDBD::mysqlpackage.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
BEGINblock. If the external author did ausethen we must populate this value before theusestatement is evaluated. Theusestatements are equivalent to arequireandimportwrapped 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
AUTOLOADto 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
newto just bless an anonymous hash with our package name. That way you won’t geterrors when he calls methods on an instance.