1Mojo::Base(3)         User Contributed Perl Documentation        Mojo::Base(3)
2
3
4

NAME

6       Mojo::Base - Minimal base class for Mojo projects
7

SYNOPSIS

9         package Cat;
10         use Mojo::Base -base;
11
12         has name => 'Nyan';
13         has ['age', 'weight'] => 4;
14
15         package Tiger;
16         use Mojo::Base 'Cat';
17
18         has friend  => sub { Cat->new };
19         has stripes => 42;
20
21         package main;
22         use Mojo::Base -strict;
23
24         my $mew = Cat->new(name => 'Longcat');
25         say $mew->age;
26         say $mew->age(3)->weight(5)->age;
27
28         my $rawr = Tiger->new(stripes => 38, weight => 250);
29         say $rawr->tap(sub { $_->friend->name('Tacgnol') })->weight;
30

DESCRIPTION

32       Mojo::Base is a simple base class for Mojo projects with fluent
33       interfaces.
34
35         # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
36         use Mojo::Base -strict;
37         use Mojo::Base -base;
38         use Mojo::Base 'SomeBaseClass';
39         use Mojo::Base -role;
40
41       All four forms save a lot of typing. Note that role support depends on
42       Role::Tiny (2.000001+).
43
44         # use Mojo::Base -strict;
45         use strict;
46         use warnings;
47         use utf8;
48         use feature ':5.10';
49         use mro;
50         use IO::Handle ();
51
52         # use Mojo::Base -base;
53         use strict;
54         use warnings;
55         use utf8;
56         use feature ':5.10';
57         use mro;
58         use IO::Handle ();
59         push @ISA, 'Mojo::Base';
60         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
61
62         # use Mojo::Base 'SomeBaseClass';
63         use strict;
64         use warnings;
65         use utf8;
66         use feature ':5.10';
67         use mro;
68         use IO::Handle ();
69         require SomeBaseClass;
70         push @ISA, 'SomeBaseClass';
71         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
72
73         # use Mojo::Base -role;
74         use strict;
75         use warnings;
76         use utf8;
77         use feature ':5.10';
78         use mro;
79         use IO::Handle ();
80         use Role::Tiny;
81         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
82
83       On Perl 5.20+ you can also append a "-signatures" flag to all three
84       forms and enable support for subroutine signatures.
85
86         # Also enable signatures
87         use Mojo::Base -strict, -signatures;
88         use Mojo::Base -base, -signatures;
89         use Mojo::Base 'SomeBaseClass', -signatures;
90         use Mojo::Base -role, -signatures;
91
92       This will also disable experimental warnings on versions of Perl where
93       this feature was still experimental.
94

FLUENT INTERFACES

96       Fluent interfaces are a way to design object-oriented APIs around
97       method chaining to create domain-specific languages, with the goal of
98       making the readablity of the source code close to written prose.
99
100         package Duck;
101         use Mojo::Base -base;
102
103         has 'name';
104
105         sub quack {
106           my $self = shift;
107           my $name = $self->name;
108           say "$name: Quack!"
109         }
110
111       Mojo::Base will help you with this by having all attribute accessors
112       created with "has" (or "attr") return their invocant ($self) whenever
113       they are used to assign a new attribute value.
114
115         Duck->new->name('Donald')->quack;
116
117       In this case the "name" attribute accessor is called on the object
118       created by "Duck->new". It assigns a new attribute value and then
119       returns the "Duck" object, so the "quack" method can be called on it
120       afterwards. These method chains can continue until one of the methods
121       called does not return the "Duck" object.
122

FUNCTIONS

124       Mojo::Base implements the following functions, which can be imported
125       with the "-base" flag or by setting a base class.
126
127   has
128         has 'name';
129         has ['name1', 'name2', 'name3'];
130         has name => 'foo';
131         has name => sub {...};
132         has ['name1', 'name2', 'name3'] => 'foo';
133         has ['name1', 'name2', 'name3'] => sub {...};
134         has name => sub {...}, weak => 1;
135         has name => undef, weak => 1;
136         has ['name1', 'name2', 'name3'] => sub {...}, weak => 1;
137
138       Create attributes for hash-based objects, just like the "attr" method.
139

METHODS

141       Mojo::Base implements the following methods.
142
143   attr
144         $object->attr('name');
145         SubClass->attr('name');
146         SubClass->attr(['name1', 'name2', 'name3']);
147         SubClass->attr(name => 'foo');
148         SubClass->attr(name => sub {...});
149         SubClass->attr(['name1', 'name2', 'name3'] => 'foo');
150         SubClass->attr(['name1', 'name2', 'name3'] => sub {...});
151         SubClass->attr(name => sub {...}, weak => 1);
152         SubClass->attr(name => undef, weak => 1);
153         SubClass->attr(['name1', 'name2', 'name3'] => sub {...}, weak => 1);
154
155       Create attribute accessors for hash-based objects, an array reference
156       can be used to create more than one at a time. Pass an optional second
157       argument to set a default value, it should be a constant or a callback.
158       The callback will be executed at accessor read time if there's no set
159       value, and gets passed the current instance of the object as first
160       argument. Accessors can be chained, that means they return their
161       invocant when they are called with an argument.
162
163       These options are currently available:
164
165       weak
166           weak => $bool
167
168         Weaken attribute reference to avoid circular references and memory
169         leaks.
170
171   new
172         my $object = SubClass->new;
173         my $object = SubClass->new(name => 'value');
174         my $object = SubClass->new({name => 'value'});
175
176       This base class provides a basic constructor for hash-based objects.
177       You can pass it either a hash or a hash reference with attribute
178       values.
179
180   tap
181         $object = $object->tap(sub {...});
182         $object = $object->tap('some_method');
183         $object = $object->tap('some_method', @args);
184
185       Tap into a method chain to perform operations on an object within the
186       chain (also known as a K combinator or Kestrel). The object will be the
187       first argument passed to the callback, and is also available as $_. The
188       callback's return value will be ignored; instead, the object (the
189       callback's first argument) will be the return value. In this way,
190       arbitrary code can be used within (i.e., spliced or tapped into) a
191       chained set of object method calls.
192
193         # Longer version
194         $object = $object->tap(sub { $_->some_method(@args) });
195
196         # Inject side effects into a method chain
197         $object->foo('A')->tap(sub { say $_->foo })->foo('B');
198
199   with_roles
200         my $new_class = SubClass->with_roles('SubClass::Role::One');
201         my $new_class = SubClass->with_roles('+One', '+Two');
202         $object       = $object->with_roles('+One', '+Two');
203
204       Create a new class with one or more Role::Tiny roles. If called on a
205       class returns the new class, or if called on an object reblesses the
206       object into the new class. For roles following the naming scheme
207       "MyClass::Role::RoleName" you can use the shorthand "+RoleName". Note
208       that role support depends on Role::Tiny (2.000001+).
209
210         # Create a new class with the role "SubClass::Role::Foo" and instantiate it
211         my $new_class = SubClass->with_roles('+Foo');
212         my $object    = $new_class->new;
213

SEE ALSO

215       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
216
217
218
219perl v5.28.0                      2018-10-18                     Mojo::Base(3)
Impressum