***************************************************** * GENERATED FILE, DO NOT EDIT * * THIS IS NO SOURCE FILE, BUT RESULT OF COMPILATION * ***************************************************** This file was generated by po4a(7). Do not store it (in VCS, for example), but store the PO file used as source file by po4a-translate. In fact, consider this as a binary, and the PO file as a regular .c file: If the PO get lost, keeping this translation up-to-date will be harder. =encoding UTF-8 =pod =head1 NAME Moose::Manual::MooseX - Moose 推荐拓展 =head1 VERSION version 2.0401 =head1 Moose 拓展? 拓展 Moose 是很容易的,而这也是使得 Moose 如此强大的原因。你可以通过 MOP API 来添加新功能,或是定制 Moose。 编写拓展需要对 meta-model 有很好的理解,你可以通过 L 来了解 meta-model,在 L 中你可以看到一些关于拓展的例子。 解释如何编写拓展这已经超出了本文档的内容。幸运的是,已经有很多人写了很多模块放在 CPAN 上来帮助我们拓展 Moose。 这篇文档包含几个我们经常使用的模块。 =head1 L 此模块功能已经包含在 Moose core 中,具体请参看 L。 =head1 L MooseX::AttributeHelpers, but turned inside out, Moose::Autobox provides methods on both arrays/hashes/etc. but also references to them, using Moose roles, allowing you do to things like: use Moose::Autobox; $somebody_elses_object->orders->push($order); 也许并不是所有的人都喜欢这个模块,但是这个模块提供了大量的语法糖,对于那些使用 API 来拓展的人来说是非常方便的。 =head1 L 默认情况下,Moose 允许你添加代码到类的构造函数中。如果你加载了 L 模块话,只要有 Moose 无法识别的代码,Moose 就会抛出异常。 package User; use Moose; use MooseX::StrictConstructor; has 'name'; has 'email'; User->new( name => 'Bob', emali => 'bob@example.com' ); 加载 L 后,错字("emali")将会引发异常,而默认情况下,这个错误是会被忽略的。 =head1 L 我们对 L 和 L 模块有很高的期望。然而在实际应用中仍然为测试版本。 如果你不愿意为此冒险,我们推荐你使用略显笨重(但是更快、更简单)的 L 模块。这会让你使用 Moose 的类型和类型约束来进行参数检验。 package User; use Moose; use MooseX::Params::Validate; sub login { my $self = shift; my ($password) = validated_list( \@_, password => { isa => 'Str', required => 1 } ); ... } =head1 L 这个模块是一个在类中添加 C 方法的角色。这个方法用命令行参数来构造。 这使得你编写一个基于命令行的程序变的非常的简单: package App::Foo; use Moose; with 'MooseX::Getopt'; has 'input' => ( is => 'ro', isa => 'Str', required => 1 ); has 'output' => ( is => 'ro', isa => 'Str', required => 1 ); sub run { ... } 在脚本中我们写如下代码: use App::Foo; App::Foo->new_with_options->run; 在命令行下,我们可以这样运行脚本: foo@example> foo --input /path/to/input --output /path/to/output =head1 L 老实说,单态是在不方便设置全局变量的语言中使用的一种技巧。 在 Perl 中,你可以很方便的使用全局变量。另外,你也可以通过 L 来定义单态。 package Config; use MooseX::Singleton; # instead of Moose has 'cache_dir' => ( ... ); 就是那么简单。 =head1 可供考虑的拓展 在 CPAN 上有很多关于 Moose 的拓展。之后我们会大概介绍一些模块,但是使用与否需要慎重考虑。 =head2 L 基于 Perl 中的 C 模块,非常酷,但是还在实验阶段。 class User { has 'name' => ( ... ); has 'email' => ( ... ); method login (Str $password) { ... } } =head2 L 这个模块可以帮助你为你的程序构建类型系统。它也可以让你预先声明类型的名称,并使用它们作为 barewords 。 use MooseX::Types -declare => ['PositiveInt']; use MooseX::Types::Moose 'Int'; subtype PositiveInt, as Int, where { $_ > 0 }, message { "Int is not larger then 0" }; 那些 bareword 注册在 Moose 的类型系统中。所以你可以在多个程序中使用相同的 bareword。 =head2 L 这个模块是在 L 的基础上创建,用来方便你声明较复杂的结构类型。 use MooseX::Types -declare => [ qw( Name Color ) ]; use MooseX::Types::Moose qw(Str Int); use MooseX::Types::Structured qw(Dict Tuple Optional); subtype Name => as Dict[ first => Str, middle => Optional[Str], last => Str ]; subtype Color => as Tuple[ Int, Int, Int, Optional[Int] ]; Of course, you could always use objects to represent these sorts of things too. =head2 L 这个拓展允许你声明类属性。 package User; use Moose; use MooseX::ClassAttribute; has 'name' => ( ... ); class_has 'Cache' => ( ... ); 需要注意的是这个类I<不>继承类似 L 类似的属性。调用 $subclass->Cache($cache); 将会设置它以及它的超类。 =head2 L 这个模块是提供大量关于 daemon 以及相应的启动关闭管理的角色。 =head2 L 这个模块可以帮助你定制一个非常具体的角色,控制消耗该角色的类需要有什么要的参数、属性、方法等。 =head2 L 这是对 C 的一个简单的封装,并且包含一些对于 C 操作的语法糖。 =head2 L 这个模块可以自动的为你的访问器以 I 的风格命名。 =head2 L 这个模块可以自动的为你的访问器设置明确的 set 方法("set_size")和隐含的 get 方法("size")。 =head2 L MooseX::NonMoose 模块允许你较简单的继承非 Moose 类。 =head1 作者 Moose 是由许多志愿者共同努力的结果。具体的请参看 L 和L译者:xiaomo(wxm4ever@gmail.com) =head1 版权和许可 This software is copyright (c) 2011 by Infinity Interactive, Inc.. 这是自由软件,您可以重新分配或根据 Perl 5 的编程语言系统本身相关的条款进行修改。