模塊

2018-02-24 16:03 更新

利用?use lib?在非標準位置搜索模塊

要搜索沒有安裝到?@INC?所指定路徑的模塊,使用?lib?編譯指令:

use lib '/home/andy/private-lib/';
use Magic::Foo;

注意:use lib?必須置于試圖使用?Magic::Foo?之前。

利用 Module::Starter 創(chuàng)建新模塊

Module::Starter?及其命令行工具?module-starter?創(chuàng)建模塊發(fā)行套件的基本 框架,以便發(fā)布到 CPAN 上。它包含基本的代碼布局、POD 指令、文檔片斷、基本 測試、Makefile.PL?和?MANIFEST?文件、以及?README?和?Changes?記錄 的開頭。

$ module-starter --module=Magic::Foo --module=Magic::Foo::Internals \
    --author="Andy Lester" --email="andy@perl.org" --verbose
Created Magic-Foo
Created Magic-Foo/lib/Magic
Created Magic-Foo/lib/Magic/Foo.pm
Created Magic-Foo/lib/Magic/Foo
Created Magic-Foo/lib/Magic/Foo/Internals.pm
Created Magic-Foo/t
Created Magic-Foo/t/pod-coverage.t
Created Magic-Foo/t/pod.t
Created Magic-Foo/t/boilerplate.t
Created Magic-Foo/t/00-load.t
Created Magic-Foo/.cvsignore
Created Magic-Foo/Makefile.PL
Created Magic-Foo/Changes
Created Magic-Foo/README
Created Magic-Foo/MANIFEST
Created starter directories and files

利用?h2xs?創(chuàng)建 XS 模塊

如果你想創(chuàng)建 XS 模塊,即 Perl 代碼與外部 C 代碼的接口,那么你將需要使用 原始的模塊開始工具?h2xs。h2xs?已包含到每個 Perl 發(fā)行中,但它可能并沒 有Module::Starter?那么新。除非你需要 XS 代碼,否則使用?Module::Starter

利用 Dist::Zilla 創(chuàng)建、打包及發(fā)行模塊

Dist::Zilla?是一個相當好用的 Perl 模塊,它使創(chuàng)建、打包、以及發(fā)行 模塊的過程變得十分容易。如果你打算將編寫的模塊發(fā)布到 CPAN 上,那么使用 Dist::Zilla 將為你節(jié)省許多時間。

初始化 Dist::Zilla 配置

安裝 Dist::Zilla 之后的第一件事就是初始化其配置:

$ dzil setup

根據(jù)向?qū)峁┠愕男彰?、Email、選擇版權(quán)許可、以及 PAUSE 帳號(發(fā)布模塊 到 CPAN 時需要)即可。

Dist::Zilla 默認將配置文件保存在?~/.dzil/config.ini?文件中,所以后續(xù) 你也可以通過修改此文件來變更相應(yīng)信息。

創(chuàng)建模塊

執(zhí)行以下命令可以創(chuàng)建一個新的模塊,如 Foo::Bar:

$ dzil new Foo::Bar

[DZ] making target dir /home/xiaodong/code/Foo-Bar
[DZ] writing files to /home/xiaodong/code/Foo-Bar
[DZ] dist minted in ./Foo-Bar

這將創(chuàng)建如下目錄結(jié)構(gòu)及文件:

Foo-Bar
├── dist.ini
└── lib
    └── Foo
            └── Bar.pm

其中,dist.ini?為該模塊的配置文件,Bar.pm?為模塊源文件。

打包模塊

待模塊編寫完畢,你就可以將模塊打包了:

$ dzil build

[DZ] beginning to build WebService-TaobaoIP
[DZ] guessing dist's main_module is lib/WebService/TaobaoIP.pm
[DZ] extracting distribution abstract from lib/WebService/TaobaoIP.pm
[DZ] writing WebService-TaobaoIP in WebService-TaobaoIP-0.03
defined(@array) is deprecated at /usr/share/perl5/Log/Log4perl/Config.pm line
864.
    (Maybe you should just omit the defined()?)
[DZ] building archive with Archive::Tar::Wrapper
[DZ] writing archive to WebService-TaobaoIP-0.03.tar.gz

執(zhí)行該命令后,模塊就會被打包成?.tar.gz?格式。

發(fā)布模塊

如果你要將模塊發(fā)布到 CPAN 上,只需執(zhí)行:

$ dzil release

[DZ] beginning to build WebService-TaobaoIP
[DZ] guessing dist's main_module is lib/WebService/TaobaoIP.pm
[DZ] extracting distribution abstract from lib/WebService/TaobaoIP.pm
[DZ] writing WebService-TaobaoIP in WebService-TaobaoIP-0.03
defined(@array) is deprecated at /usr/share/perl5/Log/Log4perl/Config.pm line
864.
    (Maybe you should just omit the defined()?)
[DZ] building archive with Archive::Tar::Wrapper
[DZ] writing archive to WebService-TaobaoIP-0.03.tar.gz
[@Basic/TestRelease] Extracting
/home/xiaodong/code/WebService-TaobaoIP/WebService-TaobaoIP-0.03.tar.gz
to .build/x8WYcGBWoY
Checking if your kit is complete...
Looks good
Writing Makefile for WebService::TaobaoIP
Writing MYMETA.yml and MYMETA.json
cp lib/WebService/TaobaoIP.pm blib/lib/WebService/TaobaoIP.pm
Manifying blib/man3/WebService::TaobaoIP.3pm
No tests defined for WebService::TaobaoIP extension.
[@Basic/TestRelease] all's well; removing .build/x8WYcGBWoY
*** Preparing to release WebService-TaobaoIP-0.03.tar.gz with
@Basic/UploadToCPAN ***

Do you want to continue the release process? [y/N]: N

根據(jù)提示,回答?y?將發(fā)布模塊,N?將終止發(fā)布過程。

其他功能

Dist::Zilla 不愧為一站式工具,除上述基本功能之外,還包括添加模塊到現(xiàn)有 發(fā)行、執(zhí)行測試、列出模塊依賴等特性。

有關(guān) Dist::Zilla 的更多用法,可參考其官方文檔。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號