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.16 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.16';
49         use mro;
50
51         # use Mojo::Base -base;
52         use strict;
53         use warnings;
54         use utf8;
55         use feature ':5.16';
56         use mro;
57         push @ISA, 'Mojo::Base';
58         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
59
60         # use Mojo::Base 'SomeBaseClass';
61         use strict;
62         use warnings;
63         use utf8;
64         use feature ':5.16';
65         use mro;
66         require SomeBaseClass;
67         push @ISA, 'SomeBaseClass';
68         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
69
70         # use Mojo::Base -role;
71         use strict;
72         use warnings;
73         use utf8;
74         use feature ':5.16';
75         use mro;
76         use Role::Tiny;
77         sub has { Mojo::Base::attr(__PACKAGE__, @_) }
78
79       On Perl 5.20+ you can also use the "-signatures" flag with all four
80       forms and enable support for subroutine signatures.
81
82         # Also enable signatures
83         use Mojo::Base -strict, -signatures;
84         use Mojo::Base -base, -signatures;
85         use Mojo::Base 'SomeBaseClass', -signatures;
86         use Mojo::Base -role, -signatures;
87
88       If you have Future::AsyncAwait 0.36+ installed you can also use the
89       "-async_await" flag to activate the "async" and "await" keywords to
90       deal much more efficiently with promises. Note that this feature is
91       EXPERIMENTAL and might change without warning!
92
93         # Also enable async/await
94         use Mojo::Base -strict, -async_await;
95         use Mojo::Base -base, -signatures, -async_await;
96
97       This will also disable experimental warnings on versions of Perl where
98       this feature was still experimental.
99

FLUENT INTERFACES

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

FUNCTIONS

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

METHODS

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

SEE ALSO

219       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
220
221
222
223perl v5.32.1                      2021-02-07                     Mojo::Base(3)
Impressum