開發 Perl Module

開發 Perl Module

將既有的 Perl 程式 開發轉換成 Perl Module 其實非常容易

而且對於以後自己或是分享給他人使用非常有幫助

建立空的 module

h2xs -n module_name

就會產生基本的 template

可以看到有這些

cd module_name

Changes fallback/ lib/ Makefile.PL MANIFEST ppport.h README t/ module_name.xs

如果有特殊需求請修改 Makefile.PL

儘量編輯 Changes and README

主程式在 lib/module_name.pm因此重點在

module_name.pm 的架構

| | | | package module_name; | | module 的名子 | | | | | | our @EXPORT = qw(); | | 提供可以被使用函數的名子 | | | | | | Preloaded methods | | 將函數撰寫於此 | | | | | | 1; | | module 程式碼到此為止 | | | | | | END | | compiler 編譯到此為止 | | | | | | =head1 | | 說明開始 | | | | | | =cut | | 說明結束 | | | |

範例:

# 你的 module 的名子, 到 "1;" 為止之間都是程式碼

package module_name;

# 如果你的程式有需要 use, 一起加在這邊吧

use 5.008008;

use strict;

use warnings;

use Carp;

require Exporter;

use AutoLoader;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export

# names by default without a very good reason. Use EXPORT_OK instead.

# Do not simply export all your public functions/methods/constants.

# This allows declaration use module_name ':all';

# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK

# will save memory.

our %EXPORT_TAGS = ( 'all' => [ qw(

) ] );

our @EXPORT_OK = ( @{ \$EXPORT_TAGS{'all'} } );

#在這邊輸入你要給別人用的 function name

our @EXPORT = qw(

hello

);

# 版本資訊,make dist 會用到

our \$VERSION = '0.01';

sub AUTOLOAD {

# This AUTOLOAD is used to 'autoload' constants from the constant()

# XS function.

my \$constname;

our \$AUTOLOAD;

(\$constname = \$AUTOLOAD) =\~ s/.*:://;

croak "&module_name::constant not defined" if \$constname eq 'constant';

my (\$error, \$val) = constant(\$constname);

if (\$error) { croak \$error; }

{

no strict 'refs';

# Fixed between 5.005_53 and 5.005_61

#XXX if (\$] >= 5.00561) {

#XXX *\$AUTOLOAD = sub () { \$val };

#XXX }

#XXX else {

*\$AUTOLOAD = sub { \$val };

#XXX }

}

goto &\$AUTOLOAD;

}

require XSLoader;

XSLoader::load('module_name', \$VERSION);

# Preloaded methods go here.

# 這邊撰寫你的程式碼

sub hello{

print "hello worldn";

}

# Autoload methods go after =cut, and are processed by the autosplit program.

# 1; 表示 module 程式 結束, 從最一開始的 "package name" 到此 " 1; " 之間就是你的主要程式碼

1;

END

# Below is stub documentation for your module. You'd better edit it!

# 開始說明 從 head 到 cut 都是說明, 依照其規則可使用許多tool將說明轉成純文字或是html, 後續會介紹

=head1 NAME

# 簡單說明用途

module_name - Perl extension for blah blah blah

=head1 SYNOPSIS

# 說明如何使用

use module_name;

module_name::hello();

blah blah blah

=head1 DESCRIPTION

# 更多詳細說明

Stub documentation for module_name, created by h2xs. It looks like the

author of the extension was negligent enough to leave the stub

unedited.

Blah blah blah.

=head2 EXPORT

None by default.

=head1 SEE ALSO

# 參考資訊,可供參考其他相關的 module

Mention other useful documentation such as the documentation of

related modules or operating system documentation (such as man pages

in UNIX), or any relevant external documentation such as RFCs or

standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

# 作者資訊

Yu-Chin Tsai, Ethomas@E

=head1 COPYRIGHT AND LICENSE

# 版權資訊

Copyright (C) 2007 by Yu-Chin Tsai

This library is free software; you can redistribute it and/or modify

it under the same terms as Perl itself, either Perl version 5.8.8 or,

at your option, any later version of Perl 5 you may have available.

# 結束說明 從 head 到 cut 都是說明

=cut

產生文件

pm 檔 可以使用 pod 轉出說明文件,相關工具有

pod2text, pod2man, pod2html, pod2latex, pod2usage.

# 產生純文字說明

\$ pod2text module_name.pm

NAME

module_name - Perl extension for blah blah blah

SYNOPSIS

use module_name;

blah blah blah

.....

# 產生 latex 格式說明文件

\$ pod2latex module_name.pm

\$ ls

module_name.pm module_name.tex

# 輸出 usage (來自 )

\$ pod2usage module_name.pm

Usage:

use module_name;

module_name::hello();

blah blah blah

編譯與安裝

在目錄下依序執行

產生make file

\$ perl Makefile.PL

Checking if your kit is complete...

Looks good

Writing Makefile for module_name

編譯

\$ make

...

chmod 755 blib/arch/auto/module_name/module_name.so

cp module_name.bs blib/arch/auto/module_name/module_name.bs

chmod 644 blib/arch/auto/module_name/module_name.bs

Manifying blib/man3/module_name.3pm

測試

\$ make test

PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t

t/module_name....ok

All tests successful.

Files=1, Tests=1, 0 wallclock secs ( 0.04 cusr + 0.01 csys = 0.05 CPU)

如果通過測試則可以繼續安裝

如果需要特別寫 test 請參考下方參考資料

進行安裝

\$ make install

發佈 module

\$ make dist

......

module_name-0.01/META.yml

module_name-0.01/module_name.xs

module_name-0.01/MANIFEST

rm -rf module_name-0.01

gzip --best module_name-0.01.tar

參考資料

http://world.std.com/\~swmcd/steven/perl/module_mechanics.html#TOC12

http://world.std.com/\~swmcd/steven/perl/module_anatomy.html

convert from Thomas blog post id 106 old convert log: ./160458/tag%3E2007%2006%20perl)

@2007 @06 @perl

Comments