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 use the "-signatures" flag with all four
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       If you have Future::AsyncAwait 0.35+ installed you can also use the
93       "-async_await" flag to activate the "async" and "await" keywords to
94       deal much more efficiently with promises. Note that this feature is
95       EXPERIMENTAL and might change without warning!
96
97         # Also enable async/await
98         use Mojo::Base -strict, -async_await;
99         use Mojo::Base -base, -signatures, -async_await;
100
101       This will also disable experimental warnings on versions of Perl where
102       this feature was still experimental.
103

FLUENT INTERFACES

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

FUNCTIONS

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

METHODS

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

SEE ALSO

224       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
225
226
227
228perl v5.30.1                      2020-01-30                     Mojo::Base(3)
Impressum