你如何为Perl编写模块?在Python中,您可以使用:
# module.py
def helloworld(name):
print "Hello, %s" % name
# main.py
import module
module.helloworld("Jim")
Run Code Online (Sandbox Code Playgroud)
jro*_*way 27
一类:
# lib/Class.pm
package Class;
use Moose;
# define the class
1;
Run Code Online (Sandbox Code Playgroud)
导出功能的模块:
# lib/A/Module.pm
package A::Module;
use strict;
use warnings;
use Sub::Exporter -setup => {
exports => [ qw/foo bar/ ],
};
sub foo { ... }
sub bar { ... }
1;
Run Code Online (Sandbox Code Playgroud)
使用这些脚本的脚本:
# 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();
Run Code Online (Sandbox Code Playgroud)
tun*_*nuz 25
基本上你创建一个名为的文件Yourmodulename.pm,其内容是:
package Yourmodulename;
# Here are your definitions
1; # Important, every module should return a true value
Run Code Online (Sandbox Code Playgroud)
然后使用该模块的程序将如下所示:
#!/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
Run Code Online (Sandbox Code Playgroud)
您可能希望以分层(并且希望是逻辑)方式组织模块.为此,您需要创建一个目录树,如:
你的/ Module.pm
你的/ Other/Module.pm
然后在你的程序中:
use Your::Module;
use Your::Other::Module;
Run Code Online (Sandbox Code Playgroud)
从模块中导出函数和变量有更多的工具,你可以看看Henning Koch的"写严肃的Perl:你需要知道的绝对最小值".
dra*_*tun 14
Perl中Python示例的"精确"等价物如下所示:
# MyModule.pm
package MyModule;
sub helloworld {
my ( $name ) = @_;
print "Hello, $name\n";
}
1;
# main.pl
use MyModule;
MyModule::helloworld( 'Jim' );
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅perlfunc文档中的条目package.更多信息,请参阅perlmod文档.
bri*_*foy 11
Intermediate Perl的最后三分之一用于创建模块.
每当你想知道如何在Perl中做某事时,请检查perltoc,Perl文档的目录:
% perldoc perltoc
Run Code Online (Sandbox Code Playgroud)
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";
}
Run Code Online (Sandbox Code Playgroud)
这并不经常使用,因为它为您提供了一个在文件中定义的包,其名称与包的名称不同,这可能会因为您必须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.
Run Code Online (Sandbox Code Playgroud)
正如其他人所说,你可以像这样使用它:
use Foo::Bar;
Run Code Online (Sandbox Code Playgroud)