未加星标

Embedding code snippets from other modules and DevelopRequires

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Aside from use()-ing or require()-ing code from other modules, you can also embed code snippets directly into your module (a single function, variable declaration, or the whole module). I’ve done this several times. For example, one time I need to remove duplicate elements from an array. Instead of using List::MoreUtils, I copy-pasted the uniq function from List::MoreUtils::PP into my module’s source code and add a note about it:

# BEGIN: stolen from List::MoreUtils::PP sub uniq (@) { my %seen = (); my $k; my $seen_undef; grep { defined $_ ? not $seen{ $k = $_ }++ : not $seen_undef++ } @_; } # END: stolen from List::MoreUtils::PP

I’ve also made a couple of Dist::Zilla plugins to help me automate this kind of process.

By embedding, you avoid the end user’s cost of having to install List::MoreUtils (which is still a non-core module at this time of writing). You also save a bit of startup/compile time by excluding the rest of the functions that you do not need. Of course, you should only do this in the special cases where you really want to minimize dependencies or startup overhead like in bootstrapping scripts or, often in my case, in tab completion scripts which must give answer fast after user presses the ~Tab~ key. And normally you should only do this for code that is already stable and proven, because the cost of having to update this embedded code is usually greater compared to if you simply depend on another module (a case in point: Module::Install).

Since this kind of code embedding is still a form of dependency (whenever the code in source module is updated, you might want to update the embedded code too), it is a good idea to express this dependency when you package your module as a Perl distribution. The appropriate phase and relationship to use for this kind of dependency is DevelopRequires. Modules listed in DevelopRequires dependency will not be installed when users install your module using a CPAN client, but the dependency will serve as a reminder/note that you still depend on the source module.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: Perl
分页:12
转载请注明
本文标题:Embedding code snippets from other modules and DevelopRequires
本站链接:http://www.codesec.net/view/484470.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(24)