## 利用?`use lib`?在非標準位置搜索模塊
要搜索沒有安裝到?`@INC`?所指定路徑的模塊,使用?`lib`?編譯指令:
~~~
use lib '/home/andy/private-lib/';
use Magic::Foo;
~~~
注意:`use lib`?必須置于試圖使用?`Magic::Foo`?之前。
## 利用 Module::Starter 創建新模塊
[Module::Starter](https://metacpan.org/module/Module::Starter)?及其命令行工具?`module-starter`?創建模塊發行套件的基本 框架,以便發布到 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`?創建 XS 模塊
如果你想創建 XS 模塊,即 Perl 代碼與外部 C 代碼的接口,那么你將需要使用 原始的模塊開始工具?`h2xs`。`h2xs`?已包含到每個 Perl 發行中,但它可能并沒 有`Module::Starter`?那么新。除非你需要 XS 代碼,否則使用?`Module::Starter`。
## 利用 Dist::Zilla 創建、打包及發行模塊
[Dist::Zilla](https://metacpan.org/module/Dist::Zilla)?是一個相當好用的 Perl 模塊,它使創建、打包、以及發行 模塊的過程變得十分容易。如果你打算將編寫的模塊發布到 CPAN 上,那么使用 Dist::Zilla 將為你節省許多時間。
### 初始化 Dist::Zilla 配置
安裝 Dist::Zilla 之后的第一件事就是初始化其配置:
~~~
$ dzil setup
~~~
根據向導提供你的姓名、Email、選擇版權許可、以及 PAUSE 帳號(發布模塊 到 CPAN 時需要)即可。
Dist::Zilla 默認將配置文件保存在?`~/.dzil/config.ini`?文件中,所以后續 你也可以通過修改此文件來變更相應信息。
### 創建模塊
執行以下命令可以創建一個新的模塊,如 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
~~~
這將創建如下目錄結構及文件:
~~~
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
~~~
執行該命令后,模塊就會被打包成?`.tar.gz`?格式。
### 發布模塊
如果你要將模塊發布到 CPAN 上,只需執行:
~~~
$ 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
~~~
根據提示,回答?`y`?將發布模塊,`N`?將終止發布過程。
### 其他功能
Dist::Zilla 不愧為一站式工具,除上述基本功能之外,還包括添加模塊到現有 發行、執行測試、列出模塊依賴等特性。
有關 Dist::Zilla 的更多用法,可參考其[官方文檔](http://dzil.org/tutorial/contents.html)。