.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Base 3pm" .TH Mojo::Base 3pm "2019-02-05" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::Base \- Minimal base class for Mojo projects .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& package Cat; \& use Mojo::Base \-base; \& \& has name => \*(AqNyan\*(Aq; \& has [\*(Aqage\*(Aq, \*(Aqweight\*(Aq] => 4; \& \& package Tiger; \& use Mojo::Base \*(AqCat\*(Aq; \& \& has friend => sub { Cat\->new }; \& has stripes => 42; \& \& package main; \& use Mojo::Base \-strict; \& \& my $mew = Cat\->new(name => \*(AqLongcat\*(Aq); \& say $mew\->age; \& say $mew\->age(3)\->weight(5)\->age; \& \& my $rawr = Tiger\->new(stripes => 38, weight => 250); \& say $rawr\->tap(sub { $_\->friend\->name(\*(AqTacgnol\*(Aq) })\->weight; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::Base is a simple base class for Mojo projects with fluent interfaces. .PP .Vb 5 \& # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features \& use Mojo::Base \-strict; \& use Mojo::Base \-base; \& use Mojo::Base \*(AqSomeBaseClass\*(Aq; \& use Mojo::Base \-role; .Ve .PP All four forms save a lot of typing. Note that role support depends on Role::Tiny (2.000001+). .PP .Vb 7 \& # use Mojo::Base \-strict; \& use strict; \& use warnings; \& use utf8; \& use feature \*(Aq:5.10\*(Aq; \& use mro; \& use IO::Handle (); \& \& # use Mojo::Base \-base; \& use strict; \& use warnings; \& use utf8; \& use feature \*(Aq:5.10\*(Aq; \& use mro; \& use IO::Handle (); \& push @ISA, \*(AqMojo::Base\*(Aq; \& sub has { Mojo::Base::attr(_\|_PACKAGE_\|_, @_) } \& \& # use Mojo::Base \*(AqSomeBaseClass\*(Aq; \& use strict; \& use warnings; \& use utf8; \& use feature \*(Aq:5.10\*(Aq; \& use mro; \& use IO::Handle (); \& require SomeBaseClass; \& push @ISA, \*(AqSomeBaseClass\*(Aq; \& sub has { Mojo::Base::attr(_\|_PACKAGE_\|_, @_) } \& \& # use Mojo::Base \-role; \& use strict; \& use warnings; \& use utf8; \& use feature \*(Aq:5.10\*(Aq; \& use mro; \& use IO::Handle (); \& use Role::Tiny; \& sub has { Mojo::Base::attr(_\|_PACKAGE_\|_, @_) } .Ve .PP On Perl 5.20+ you can also append a \f(CW\*(C`\-signatures\*(C'\fR flag to all three forms and enable support for subroutine signatures. .PP .Vb 5 \& # Also enable signatures \& use Mojo::Base \-strict, \-signatures; \& use Mojo::Base \-base, \-signatures; \& use Mojo::Base \*(AqSomeBaseClass\*(Aq, \-signatures; \& use Mojo::Base \-role, \-signatures; .Ve .PP This will also disable experimental warnings on versions of Perl where this feature was still experimental. .SH "FLUENT INTERFACES" .IX Header "FLUENT INTERFACES" Fluent interfaces are a way to design object-oriented APIs around method chaining to create domain-specific languages, with the goal of making the readablity of the source code close to written prose. .PP .Vb 2 \& package Duck; \& use Mojo::Base \-base; \& \& has \*(Aqname\*(Aq; \& \& sub quack { \& my $self = shift; \& my $name = $self\->name; \& say "$name: Quack!" \& } .Ve .PP Mojo::Base will help you with this by having all attribute accessors created with \*(L"has\*(R" (or \*(L"attr\*(R") return their invocant (\f(CW$self\fR) whenever they are used to assign a new attribute value. .PP .Vb 1 \& Duck\->new\->name(\*(AqDonald\*(Aq)\->quack; .Ve .PP In this case the \f(CW\*(C`name\*(C'\fR attribute accessor is called on the object created by \&\f(CW\*(C`Duck\->new\*(C'\fR. It assigns a new attribute value and then returns the \f(CW\*(C`Duck\*(C'\fR object, so the \f(CW\*(C`quack\*(C'\fR method can be called on it afterwards. These method chains can continue until one of the methods called does not return the \f(CW\*(C`Duck\*(C'\fR object. .SH "FUNCTIONS" .IX Header "FUNCTIONS" Mojo::Base implements the following functions, which can be imported with the \f(CW\*(C`\-base\*(C'\fR flag or by setting a base class. .SS "has" .IX Subsection "has" .Vb 9 \& has \*(Aqname\*(Aq; \& has [\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq]; \& has name => \*(Aqfoo\*(Aq; \& has name => sub {...}; \& has [\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => \*(Aqfoo\*(Aq; \& has [\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => sub {...}; \& has name => sub {...}, weak => 1; \& has name => undef, weak => 1; \& has [\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => sub {...}, weak => 1; .Ve .PP Create attributes for hash-based objects, just like the \*(L"attr\*(R" method. .SH "METHODS" .IX Header "METHODS" Mojo::Base implements the following methods. .SS "attr" .IX Subsection "attr" .Vb 10 \& $object\->attr(\*(Aqname\*(Aq); \& SubClass\->attr(\*(Aqname\*(Aq); \& SubClass\->attr([\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq]); \& SubClass\->attr(name => \*(Aqfoo\*(Aq); \& SubClass\->attr(name => sub {...}); \& SubClass\->attr([\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => \*(Aqfoo\*(Aq); \& SubClass\->attr([\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => sub {...}); \& SubClass\->attr(name => sub {...}, weak => 1); \& SubClass\->attr(name => undef, weak => 1); \& SubClass\->attr([\*(Aqname1\*(Aq, \*(Aqname2\*(Aq, \*(Aqname3\*(Aq] => sub {...}, weak => 1); .Ve .PP Create attribute accessors for hash-based objects, an array reference can be used to create more than one at a time. Pass an optional second argument to set a default value, it should be a constant or a callback. The callback will be executed at accessor read time if there's no set value, and gets passed the current instance of the object as first argument. Accessors can be chained, that means they return their invocant when they are called with an argument. .PP These options are currently available: .IP "weak" 2 .IX Item "weak" .Vb 1 \& weak => $bool .Ve .Sp Weaken attribute reference to avoid circular references and memory leaks. .SS "new" .IX Subsection "new" .Vb 3 \& my $object = SubClass\->new; \& my $object = SubClass\->new(name => \*(Aqvalue\*(Aq); \& my $object = SubClass\->new({name => \*(Aqvalue\*(Aq}); .Ve .PP This base class provides a basic constructor for hash-based objects. You can pass it either a hash or a hash reference with attribute values. .SS "tap" .IX Subsection "tap" .Vb 3 \& $object = $object\->tap(sub {...}); \& $object = $object\->tap(\*(Aqsome_method\*(Aq); \& $object = $object\->tap(\*(Aqsome_method\*(Aq, @args); .Ve .PP Tap into a method chain to perform operations on an object within the chain (also known as a K combinator or Kestrel). The object will be the first argument passed to the callback, and is also available as \f(CW$_\fR. The callback's return value will be ignored; instead, the object (the callback's first argument) will be the return value. In this way, arbitrary code can be used within (i.e., spliced or tapped into) a chained set of object method calls. .PP .Vb 2 \& # Longer version \& $object = $object\->tap(sub { $_\->some_method(@args) }); \& \& # Inject side effects into a method chain \& $object\->foo(\*(AqA\*(Aq)\->tap(sub { say $_\->foo })\->foo(\*(AqB\*(Aq); .Ve .SS "with_roles" .IX Subsection "with_roles" .Vb 3 \& my $new_class = SubClass\->with_roles(\*(AqSubClass::Role::One\*(Aq); \& my $new_class = SubClass\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); \& $object = $object\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); .Ve .PP Create a new class with one or more Role::Tiny roles. If called on a class returns the new class, or if called on an object reblesses the object into the new class. For roles following the naming scheme \f(CW\*(C`MyClass::Role::RoleName\*(C'\fR you can use the shorthand \f(CW\*(C`+RoleName\*(C'\fR. Note that role support depends on Role::Tiny (2.000001+). .PP .Vb 3 \& # Create a new class with the role "SubClass::Role::Foo" and instantiate it \& my $new_class = SubClass\->with_roles(\*(Aq+Foo\*(Aq); \& my $object = $new_class\->new; .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .