1Moose::Cookbook::ExtendUisnegr::CMoMonootosrseieib:su:htC_eoMdookoPbseoeroSlku:gD:aoErcx(ut3me)enndtiantgi:o:nMooseish_MooseSugar(3)
2
3
4
6 Moose::Cookbook::Extending::Mooseish_MooseSugar - Acting like Moose.pm
7 and providing sugar Moose-style
8
10 version 2.2012
11
13 package MyApp::Mooseish;
14
15 use Moose::Exporter;
16
17 Moose::Exporter->setup_import_methods(
18 with_meta => ['has_table'],
19 class_metaroles => {
20 class => ['MyApp::Meta::Class::Trait::HasTable'],
21 },
22 );
23
24 sub has_table {
25 my $meta = shift;
26 $meta->table(shift);
27 }
28
29 package MyApp::Meta::Class::Trait::HasTable;
30 use Moose::Role;
31
32 has table => (
33 is => 'rw',
34 isa => 'Str',
35 );
36
38 This recipe expands on the use of Moose::Exporter we saw in
39 Moose::Cookbook::Extending::ExtensionOverview and the class metaclass
40 trait we saw in Moose::Cookbook::Meta::Table_MetaclassTrait. In this
41 example we provide our own metaclass trait, and we also export a
42 "has_table" sugar function.
43
44 The "with_meta" parameter specifies a list of functions that should be
45 wrapped before exporting. The wrapper simply ensures that the importing
46 package's appropriate metaclass object is the first argument to the
47 function, so we can do "my $meta = shift;".
48
49 See the Moose::Exporter docs for more details on its API.
50
52 The purpose of all this code is to provide a Moose-like interface.
53 Here's what it would look like in actual use:
54
55 package MyApp::User;
56
57 use namespace::autoclean;
58
59 use Moose;
60 use MyApp::Mooseish;
61
62 has_table 'User';
63
64 has 'username' => ( is => 'ro' );
65 has 'password' => ( is => 'ro' );
66
67 sub login { ... }
68
70 Providing sugar functions can make your extension look much more Moose-
71 ish. See Fey::ORM for a more extensive example.
72
74 · Stevan Little <stevan.little@iinteractive.com>
75
76 · Dave Rolsky <autarch@urth.org>
77
78 · Jesse Luehrs <doy@tozt.net>
79
80 · Shawn M Moore <code@sartak.org>
81
82 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
83
84 · Karen Etheridge <ether@cpan.org>
85
86 · Florian Ragwitz <rafl@debian.org>
87
88 · Hans Dieter Pearcey <hdp@weftsoar.net>
89
90 · Chris Prather <chris@prather.org>
91
92 · Matt S Trout <mst@shadowcat.co.uk>
93
95 This software is copyright (c) 2006 by Infinity Interactive, Inc.
96
97 This is free software; you can redistribute it and/or modify it under
98 the same terms as the Perl 5 programming language system itself.
99
100
101
102perl v5.30.1 Moose:2:0C2o0o-k0b1o-o3k0::Extending::Mooseish_MooseSugar(3)