簡體   English   中英

你如何創建一個Perl模塊?

[英]How do you create a Perl module?

你如何為Perl編寫模塊? 在Python中,您可以使用:

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

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

一類:

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

# define the class

1;

導出功能的模塊:

# 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();

基本上你創建了一個名為Yourmodulename.pm的文件,其內容是:

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的“寫嚴肅的Perl:你需要知道的絕對最小值”

Perl中Python示例的“精確”等價物如下所示:

# MyModule.pm
package MyModule;

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

1;

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

有關更多信息,請參閱perlfunc文檔中的package條目 更多信息,請參閱perlmod文檔。

Intermediate Perl的最后三分之一用於創建模塊。

每當你想知道如何在Perl中做某事時,請檢查perltoc,Perl文檔的目錄:

% perldoc perltoc

Perl核心文檔的幾個部分可以幫助您:

祝好運,

到目前為止,答案中沒有提到的一個小細節是,如果你有一個(最好是小的)模塊,它是特定於目的的,永遠不會被重用,你可以將它放在與主程序相同的文件中或另一個包:

# 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; 在文件的末尾,而不是在每個包之后。

設置模塊的最傳統方式如下:

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.

正如其他人所說,你可以像這樣使用它:

use Foo::Bar;
cpanm Module::Starter::PBP
perl -MModule::Starter::PBP=setup
module-starter --module=My::Module

h2xs -XA -n My :: Module

h2xs是一個標准的perl實用程序,用於幫助構建鏈接的模塊,包括鏈接的C頭文件/代碼,但可用於構建純perl模塊的完整框架(帶-XA標志),包括東西像測試目錄,README文件,Makefile和Manifest。 (一篇很好的文章概述了這里的細節: http//perltraining.com.au/tips/2005-09-26.html

這有點老派,但是值得關注的是即使只是為了所有的提醒,它讓你把所有事情都弄好了(測試,文檔,版本號,導出和導出列表,所有容易忘記的東西......)

你最終會在“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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM