2009-02-15 237 views
16

你如何为Perl编写模块?在Python中,你可以使用:你如何创建一个Perl模块?

# module.py 
def helloworld(name): 
    print "Hello, %s" % name 

# main.py 
import module 
module.helloworld("Jim") 

回答

27

类:

# lib/A/Module.pm 
package A::Module; 
use strict; 
use warnings; 
use Sub::Exporter -setup => { 
    exports => [ qw/foo bar/ ], 
}; 

sub foo { ... } 
sub bar { ... } 

1; 

一个脚本,使用这些:

# bin/script.pl 
#!/usr/bin/env perl 

use strict; 
use warnings; 

use FindBin qw($Bin); 
use lib "$Bin/../lib"; 

use Class; 
use A::Module qw(foo bar); 


print Class->new; 
print foo(), bar(); 
+0

有趣的......在我见过的所有模块,做出口的方式已经机智h“我们的@ISA qw(出口商);我们的@EXPORT = qw(funcx funcy funcz)“,即使在PBP中。 – 2009-02-18 00:30:28

+4

出口商是内置的,但它很烂S :: Ex很...性感:) – jrockway 2009-02-18 01:29:04

+2

真的。 :Exporter with your module。Dependencies suck!(但是我猜如果你使用的是Moose,你已经走上了这条路!) – 2009-02-18 04:28:22

24

基本上

# lib/Class.pm 
package Class; 
use Moose; 

# define the class 

1; 

是出口功能的模块您创建一个名为的文件,其内容是:

package Yourmodulename; 

# Here are your definitions 

1; # Important, every module should return a true value 

然后使用该模块的程序是这样的:

#!/usr/bin/perl 
use strict;   # These are good pragmas 
use warnings;  

# Used modules 
use Carp;    # A module that you'll probably find useful 
use Yourmodulename; # Your module 

您可能要在组织分层(希望逻辑),这样你的模块。要做到这一点,你创建一个类似的目录树:

你/ Module.pm
你/其它/ Module.pm

然后在你的程序:

use Your::Module; 
use Your::Other::Module; 

还有更多的设施从模块中导出函数和变量,你可以看看Henning Koch的"Writing serious Perl: The absolute minimum you need to know"

14

“精确” 在Perl的Python例子相当于是这样的:

# MyModule.pm 
package MyModule; 

sub helloworld { 
    my ($name) = @_; 
    print "Hello, $name\n"; 
} 

1; 

# main.pl 
use MyModule; 
MyModule::helloworld('Jim'); 

如需更多信息,请参阅the entry for package in perlfunc文档。有关更多信息,请参见perlmod文档。

6

一个小细节,答案到目前为止还没有提到的是,如果你有一个(最好是较小的)模块,该模块是专门不够具体,它永远不会被重用,你可以把它放到同一个文件主程序或另一个包:

# main.pl 

# Since this is a beginner question, I'll also point out that you should 
# *always* use strict and warnings. It will save you many headaches. 
use strict; 
use warnings; 

MyModule::helloworld('Jim'); 
AnotherModule::helloworld('Jim'); 

package MyModule; # Still in main.pl! 

sub helloworld { 
    my ($name) = @_; 
    print "Hello, $name\n"; 
} 

package AnotherModule; # Yep, still main.pl 

sub helloworld { 
    my $name = shift; 
    print "Another hello to $name\n"; 
} 

这是不经常使用,因为它给了你,在他的名字是不一样的软件包的文件中定义的包,这可能会比较混乱,因为你必须use/require的文件名,但在程序包名称中引用它。

另请注意,1;仅作为包含在use/require中的每个文件的最后一行。在这种情况下,我不需要它,因为它在main.pl。如果您将多个软件包放入同一个文件中,则只需要在文件末尾有一个1;,而不是在每个软件包之后。

5

建立一个模块的最传统的方式如下:

package Foo::Bar; 
our @ISA  = qw(Exporter);  # Tells perl what to do with... 
our @EXPORT = qw(sub1 sub2 sub3); # automatically exported subs 
our @EXPORT_OK = qw(sub4 sub5);  # exported only when demanded 

# code for subs, constants, package variables here 

1; # Doesn't actually have to be 1, just a 'true' value. 

和行吟诗人RS说,你可以用它像这样:

use Foo::Bar; 
2

h2xs的-XA -n我::模块

是h2xs的附带Perl作为标准,用于建立连接的模块,包括辅助工具链接的C头文件/代码,但可用于构建纯Perl模块的完整框架(带-XA标志),包括像测试目录,README文件,Makefile和Manifest之类的东西。 (好文章,概述这里的细节:http://perltraining.com.au/tips/2005-09-26.html

它有点老派,但是这是值得的,即使只是为所有的提醒它gves您关于得到的一切权利(测试,文档,版本号,出口和export_ok列表,所有容易忘记的东西......)

你将在“My”目录(从“My :: Module”)目录中看到一个“Module.pm”文件,像这样:

package My::Module; 

use 5.008008; 
use strict; 
use warnings; 

require Exporter; 

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 My::Module ':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'} }); 

our @EXPORT = qw(

); 

our $VERSION = '0.01'; 


# Preloaded methods go here. 

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

=head1 NAME 

My::Module - Perl extension for blah blah blah 
2
cpanm Module::Starter::PBP 
perl -MModule::Starter::PBP=setup 
module-starter --module=My::Module