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;
107
108         has 'name';
109
110         sub quack {
111           my $self = shift;
112           my $name = $self->name;
113           say "$name: Quack!"
114         }
115
116       Mojo::Base will help you with this by having all attribute accessors
117       created with "has" (or "attr") return their invocant ($self) whenever
118       they are used to assign a new attribute value.
119
120         Duck->new->name('Donald')->quack;
121
122       In this case the "name" attribute accessor is called on the object
123       created by "Duck->new". It assigns a new attribute value and then
124       returns the "Duck" object, so the "quack" method can be called on it
125       afterwards. These method chains can continue until one of the methods
126       called does not return the "Duck" object.
127

FUNCTIONS

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

METHODS

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

SEE ALSO

220       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
221
222
223
224perl v5.32.0                      2020-07-28                     Mojo::Base(3)
Impressum