问题
I find a module, that I want to change.
My problem have some features like this:
- I want to add functionality and flexibility to this module.
- Now this module solves tasks, but web-service, for what it was written, change API
- And also, I want to use code of this module.
- It is not my module
- Fix some bugs
How i should be in this situation?
- Inheriting from this module and add functionality and upload to CPAN?
- Ask author about my modifications (and reload module)?
- Something else?
回答1:
There are various ways to modify a module as you use it, and I cover most of them in Mastering Perl.
- As Dave Cross mentions, send fixes upstream or become part of that project. It sounds like you have the ambition to be a significant contributor. :)
- Create a subclass to replace methods
- Override or overload subroutines or methods
- Wrap subroutines to modify or adapt either inputs or outputs (e.g. Hook::LexWrap)
- Create a locally patched version, and store it separately from the main code so it doesn't disappear in an upgrade
For example, this is something I often do directly in program code while I wait for an upstream fix:
use Some::Module; # load the original first
BEGIN {
package Some::Module;
no warnings 'redefine';
if( $VERSION > 1.23 and $VERSION < 1.45 ) {
*broken = sub { ... fixed version ... };
}
}
This way, I have the fix even if the target module is upgraded.
回答2:
I think that your a and b options are pretty much the best approach - although I'd probably do them the other way round.
Approach the module author with your suggestions. Read the module documentation to find out how the author likes to be contacted. Some like email, some like RT, other have more specific methods. Authors tend to like suggestions better if they come with tests for the new/changed code and patches that can be applied freely. Perhaps the code is on Github. Can you fork it, make your changes and send the author a pull request?
If the author is unresponsive, then consider either forking or subclassing their code. In this case, naming is important as you want people to be able to find your module as well as the original one. You'll also want to carefully document the differences between your version and the original one so that people can choose which one they want.
来源:https://stackoverflow.com/questions/11699118/perl-cpan-module-modifying-and-adding-functionality