你如何创建一个 Perl 模块? [英] How do you create a Perl module?

查看:24
本文介绍了你如何创建一个 Perl 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何为 Perl 编写模块?在 Python 中,您可以使用:

How do you write a module for Perl? In Python you can use:

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

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

推荐答案

A 类:

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

这篇关于你如何创建一个 Perl 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆