1Moose::Cookbook::LegacyU:s:eTrabCloen_tMCrolioabssuest:Me:edCtoaPoceklrbalososDk(o:3c:)uLmeegnatcayt:i:oTnable_ClassMetaclass(3)
2
3
4
6 Moose::Cookbook::Legacy::Table_ClassMetaclass - Adding a "table"
7 attribute to the metaclass
8
10 version 2.2015
11
13 package MyApp::Meta::Class;
14 use Moose;
15 extends 'Moose::Meta::Class';
16
17 has table => (
18 is => 'rw',
19 isa => 'Str',
20 );
21
23 WARNING: Subclassing metaclasses (as opposed to providing metaclass
24 traits) is strongly discouraged. This recipe is provided solely for
25 reference when encountering older code that does this.
26
27 In this recipe, we'll create a new metaclass which has a "table"
28 attribute. This metaclass is for classes associated with a DBMS table,
29 as one might do for an ORM.
30
31 In this example, the table name is just a string, but in a real ORM the
32 table might be an object describing the table.
33
35 This really is as simple as the recipe "SYNOPSIS" shows. The trick is
36 getting your classes to use this metaclass, and providing some sort of
37 sugar for declaring the table. This is covered in
38 Moose::Cookbook::Extending::Recipe2, which shows how to make a module
39 like "Moose.pm" itself, with sugar like "has_table()".
40
41 Using this Metaclass in Practice
42 Accessing this new "table" attribute is quite simple. Given a class
43 named "MyApp::User", we could simply write the following:
44
45 my $table = MyApp::User->meta->table;
46
47 As long as "MyApp::User" has arranged to use "MyApp::Meta::Class" as
48 its metaclass, this method call just works. If we want to be more
49 careful, we can check the metaclass's class:
50
51 $table = MyApp::User->meta->table
52 if MyApp::User->meta->isa('MyApp::Meta::Class');
53
55 Creating custom metaclass is trivial. Using it is a little harder, and
56 is covered in other recipes. We will also talk about applying traits to
57 a class metaclass, which is a more flexible and cooperative
58 implementation.
59
61 Moose::Cookbook::Meta::Recipe5 - The "table" attribute implemented as a
62 metaclass trait
63
64 Moose::Cookbook::Extending::Recipe2 - Acting like Moose.pm and
65 providing sugar Moose-style
66
68 • Stevan Little <stevan@cpan.org>
69
70 • Dave Rolsky <autarch@urth.org>
71
72 • Jesse Luehrs <doy@cpan.org>
73
74 • Shawn M Moore <sartak@cpan.org>
75
76 • יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
77
78 • Karen Etheridge <ether@cpan.org>
79
80 • Florian Ragwitz <rafl@debian.org>
81
82 • Hans Dieter Pearcey <hdp@cpan.org>
83
84 • Chris Prather <chris@prather.org>
85
86 • Matt S Trout <mstrout@cpan.org>
87
89 This software is copyright (c) 2006 by Infinity Interactive, Inc.
90
91 This is free software; you can redistribute it and/or modify it under
92 the same terms as the Perl 5 programming language system itself.
93
94
95
96perl v5.34.0 Moos2e0:2:1C-o0o7k-b2o2ok::Legacy::Table_ClassMetaclass(3)