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の最後の 3 分の 1 は、モジュールの作成に専念しています。
Perl で何かを行う方法を知りたいときはいつでも、Perl ドキュメントの目次である perltoc を確認してください。
% perldoc perltoc
コア Perl ドキュメントのいくつかの部分は、次のことに役立ちます。
幸運を、
これまでの回答で言及されていない小さな詳細の1つは、再利用されないほど目的に特化した(できれば小さい)モジュールがある場合、それをメインプログラムと同じファイルに配置するか、別のパッケージ:
# 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::モジュール
h2xs は perl に標準で付属するユーティリティで、リンクされた C ヘッダー/コードを含むリンクされたモジュールの構築を支援することを目的としていますが、純粋な perl モジュール (-XA フラグを使用) の完全なスケルトンを構築するために使用できます。テスト ディレクトリ、README ファイル、Makefile、マニフェストなどです。(詳細を概説した良い記事: http://perltraining.com.au/tips/2005-09-26.html )
ちょっと古めかしいですが、すべてを適切に行うためのリマインダー (テスト、ドキュメント、バージョン番号、export および 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