| 1 | package Mars::Class; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.018; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # IMPORT | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 53 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||
| 11 | 53 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 53 | my $from = caller; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 53 | require Mars::Kind::Class; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 53 | ▶ | my %exports = map +($_,$_), @args ? @args : qw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | attr | ||||||||||||||||||||||||||||
| 23 | base | ||||||||||||||||||||||||||||
| 24 | false | ||||||||||||||||||||||||||||
| 25 | from | ||||||||||||||||||||||||||||
| 26 | mixin | ||||||||||||||||||||||||||||
| 27 | role | ||||||||||||||||||||||||||||
| 28 | test | ||||||||||||||||||||||||||||
| 29 | true | ||||||||||||||||||||||||||||
| 30 | with | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 53 | @{"${from}::ISA"} = 'Mars::Kind::Class'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 53 | ▶ | if ($exports{"attr"} && !*{"${from}::attr"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
MC/DC50%1/2
$exports{"attr"} and not *{"${from}::attr";}{"CODE"}
$exports{"attr"}
not *{"${from}::attr";}{"CODE"}
Truth table66%2/3
A and not(B)
A= $exports{"attr"}B= not *{"${from}::attr";}{"CODE"}
| |||||||||||||||||||||||||||||
| 36 | 17 | ▶ | *{"${from}::attr"} = sub {@_ = ($from, @_); goto \&attr}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 53 | ▶ | if ($exports{"base"} && !*{"${from}::base"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"base"} and not *{"${from}::base";}{"CODE"}
MC/DC100%2/2
$exports{"base"} and not *{"${from}::base";}{"CODE"}
$exports{"base"}
not *{"${from}::base";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"base"}B= not *{"${from}::base";}{"CODE"}
| |||||||||||||||||||||||||||||
| 39 | 14 | ▶ | *{"${from}::base"} = sub {@_ = ($from, @_); goto \&base}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 53 | ▶ | if (!*{"${from}::false"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 17 | ▶ | *{"${from}::false"} = sub {require Mars; Mars::false()}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 53 | ▶ | if ($exports{"from"} && !*{"${from}::from"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"from"} and not *{"${from}::from";}{"CODE"}
MC/DC100%2/2
$exports{"from"} and not *{"${from}::from";}{"CODE"}
$exports{"from"}
not *{"${from}::from";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"from"}B= not *{"${from}::from";}{"CODE"}
| |||||||||||||||||||||||||||||
| 45 | 14 | ▶ | *{"${from}::from"} = sub {@_ = ($from, @_); goto \&from}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 53 | ▶ | if ($exports{"mixin"} && !*{"${from}::mixin"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"mixin"} and not *{"${from}::mixin";}{"CODE"}
MC/DC100%2/2
$exports{"mixin"} and not *{"${from}::mixin";}{"CODE"}
$exports{"mixin"}
not *{"${from}::mixin";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"mixin"}B= not *{"${from}::mixin";}{"CODE"}
| |||||||||||||||||||||||||||||
| 48 | 14 | ▶ | *{"${from}::mixin"} = sub {@_ = ($from, @_); goto \&mixin}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 53 | ▶ | if ($exports{"role"} && !*{"${from}::role"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"role"} and not *{"${from}::role";}{"CODE"}
MC/DC100%2/2
$exports{"role"} and not *{"${from}::role";}{"CODE"}
$exports{"role"}
not *{"${from}::role";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"role"}B= not *{"${from}::role";}{"CODE"}
| |||||||||||||||||||||||||||||
| 51 | 14 | ▶ | *{"${from}::role"} = sub {@_ = ($from, @_); goto \&role}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 53 | ▶ | if ($exports{"test"} && !*{"${from}::test"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"test"} and not *{"${from}::test";}{"CODE"}
MC/DC100%2/2
$exports{"test"} and not *{"${from}::test";}{"CODE"}
$exports{"test"}
not *{"${from}::test";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"test"}B= not *{"${from}::test";}{"CODE"}
| |||||||||||||||||||||||||||||
| 54 | 14 | ▶ | *{"${from}::test"} = sub {@_ = ($from, @_); goto \&test}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 53 | ▶ | if (!*{"${from}::true"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 17 | ▶ | *{"${from}::true"} = sub {require Mars; Mars::true()}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 53 | ▶ | if ($exports{"with"} && !*{"${from}::with"}{"CODE"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exports{"with"} and not *{"${from}::with";}{"CODE"}
MC/DC100%2/2
$exports{"with"} and not *{"${from}::with";}{"CODE"}
$exports{"with"}
not *{"${from}::with";}{"CODE"}
Truth table100%3/3
A and not(B)
A= $exports{"with"}B= not *{"${from}::with";}{"CODE"}
| |||||||||||||||||||||||||||||
| 60 | 14 | ▶ | *{"${from}::with"} = sub {@_ = ($from, @_); goto \&test}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 53 | ${"${from}::META"} = {}; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 53 | return $self; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 21 | ▶ | sub attr { | ||||||||||||||||||||||||||
|
Subroutine
attr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 21 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 21 | $from->ATTR(@args); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 21 | return $from; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | ▶ | sub base { | ||||||||||||||||||||||||||
|
Subroutine
base: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 7 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | $from->BASE(@args); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | return $from; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | ▶ | sub from { | ||||||||||||||||||||||||||
|
Subroutine
from: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 3 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | $from->FROM(@args); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | return $from; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub mixin { | ||||||||||||||||||||||||||
|
Subroutine
mixin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | $from->MIXIN(@args); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | return $from; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub role { | ||||||||||||||||||||||||||
|
Subroutine
role: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 6 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | $from->ROLE(@args); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 6 | return $from; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 21 | ▶ | sub test { | ||||||||||||||||||||||||||
|
Subroutine
test: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 21 | my ($from, @args) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 21 | $from->TEST(@args); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 18 | return $from; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 NAME | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Mars::Class - Class Declaration | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 ABSTRACT | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Class Declaration for Perl 5 | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | package Person; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | use Mars::Class 'attr'; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | attr 'fname'; | ||||||||||||||||||||||||||||
| 139 | attr 'lname'; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | package Identity; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | use Mars::Role 'attr'; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | attr 'id'; | ||||||||||||||||||||||||||||
| 146 | attr 'login'; | ||||||||||||||||||||||||||||
| 147 | attr 'password'; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub EXPORT { | ||||||||||||||||||||||||||||
| 150 | # explicitly declare routines to be consumed | ||||||||||||||||||||||||||||
| 151 | ['id', 'login', 'password'] | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | package Authenticable; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | use Mars::Role; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub authenticate { | ||||||||||||||||||||||||||||
| 159 | return true; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub AUDIT { | ||||||||||||||||||||||||||||
| 163 | my ($self, $from) = @_; | ||||||||||||||||||||||||||||
| 164 | # ensure the caller has a login and password when consumed | ||||||||||||||||||||||||||||
| 165 | die "${from} missing the login attribute" if !$from->can('login'); | ||||||||||||||||||||||||||||
| 166 | die "${from} missing the password attribute" if !$from->can('password'); | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub BUILD { | ||||||||||||||||||||||||||||
| 170 | my ($self, $data) = @_; | ||||||||||||||||||||||||||||
| 171 | $self->{auth} = undef; | ||||||||||||||||||||||||||||
| 172 | return $self; | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | sub EXPORT { | ||||||||||||||||||||||||||||
| 176 | # explicitly declare routines to be consumed | ||||||||||||||||||||||||||||
| 177 | ['authenticate'] | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | package User; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | use Mars::Class; | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | base 'Person'; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | with 'Identity'; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | attr 'email'; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | test 'Authenticable'; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub valid { | ||||||||||||||||||||||||||||
| 193 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 194 | return $self->login && $self->password ? true : false; | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | package main; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | my $user = User->new( | ||||||||||||||||||||||||||||
| 200 | fname => 'Elliot', | ||||||||||||||||||||||||||||
| 201 | lname => 'Alderson', | ||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # bless({fname => 'Elliot', lname => 'Alderson'}, 'User') | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This package provides a class builder which when used causes the consumer to | ||||||||||||||||||||||||||||
| 211 | inherit from L<Mars::Kind::Class> which provides object construction and | ||||||||||||||||||||||||||||
| 212 | lifecycle L<hooks|Mars::Kind>. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | This package provides the following functions: | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 attr | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | attr(Str $name) (Str) | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | The attr function creates attribute accessors for the calling package. This | ||||||||||||||||||||||||||||
| 227 | function is always exported unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =over 4 | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =item attr example 1 | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | package Example; | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | use Mars::Class; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | attr 'name'; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # "Example" | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =back | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head2 base | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | base(Str $name) (Str) | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | The base function registers one or more base classes for the calling package. | ||||||||||||||||||||||||||||
| 252 | This function is always exported unless a routine of the same name already | ||||||||||||||||||||||||||||
| 253 | exists. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =over 4 | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =item base example 1 | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | package Entity; | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | use Mars::Class; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | sub output { | ||||||||||||||||||||||||||||
| 266 | return; | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | package Example; | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | use Mars::Class; | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | base 'Entity'; | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # "Example" | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =back | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 false | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | false() (Bool) | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | The false function returns a falsy boolean value which is designed to be | ||||||||||||||||||||||||||||
| 286 | practically indistinguishable from the conventional numerical C<0> value. This | ||||||||||||||||||||||||||||
| 287 | function is always exported unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =over 4 | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =item false example 1 | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | package Example; | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | use Mars::Class; | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | my $false = false; | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # 0 | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =back | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =over 4 | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =item false example 2 | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | package Example; | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | use Mars::Class; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | my $true = !false; | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # 1 | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =back | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =cut | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =head2 from | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | from(Str $name) (Str) | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | The from function registers one or more base classes for the calling package | ||||||||||||||||||||||||||||
| 326 | and performs an L<"audit"|Mars::Kind/AUDIT>. This function is always exported | ||||||||||||||||||||||||||||
| 327 | unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | I<Since C<0.03>> | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =over 4 | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =item from example 1 | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | package Entity; | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | use Mars::Class; | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | sub AUDIT { | ||||||||||||||||||||||||||||
| 340 | my ($self, $from) = @_; | ||||||||||||||||||||||||||||
| 341 | die "Missing startup" if !$from->can('startup'); | ||||||||||||||||||||||||||||
| 342 | die "Missing shutdown" if !$from->can('shutdown'); | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | package Example; | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | use Mars::Class; | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | attr 'startup'; | ||||||||||||||||||||||||||||
| 350 | attr 'shutdown'; | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | from 'Entity'; | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # "Example" | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =back | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =cut | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head2 mixin | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | mixin(Str $name) (Str) | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | The mixin function registers and consumes mixins for the calling package. This | ||||||||||||||||||||||||||||
| 365 | function is always exported unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | I<Since C<0.05>> | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =over 4 | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =item mixin example 1 | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | package YesNo; | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | use Mars::Mixin; | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | sub no { | ||||||||||||||||||||||||||||
| 378 | return 0; | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | sub yes { | ||||||||||||||||||||||||||||
| 382 | return 1; | ||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | sub EXPORT { | ||||||||||||||||||||||||||||
| 386 | ['no', 'yes'] | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | package Example; | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | use Mars::Class; | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | mixin 'YesNo'; | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # "Example" | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =back | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =cut | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head2 role | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | role(Str $name) (Str) | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | The role function registers and consumes roles for the calling package. This | ||||||||||||||||||||||||||||
| 406 | function is always exported unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =over 4 | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =item role example 1 | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | package Ability; | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | use Mars::Role; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | sub action { | ||||||||||||||||||||||||||||
| 419 | return; | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | package Example; | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | use Mars::Class; | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | role 'Ability'; | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | # "Example" | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =back | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =over 4 | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item role example 2 | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | package Ability; | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | use Mars::Role; | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | sub action { | ||||||||||||||||||||||||||||
| 441 | return; | ||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | sub EXPORT { | ||||||||||||||||||||||||||||
| 445 | return ['action']; | ||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | package Example; | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | use Mars::Class; | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | role 'Ability'; | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # "Example" | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =back | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =cut | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =head2 test | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | test(Str $name) (Str) | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | The test function registers and consumes roles for the calling package and | ||||||||||||||||||||||||||||
| 465 | performs an L<"audit"|Mars::Kind/AUDIT>, effectively allowing a role to act as | ||||||||||||||||||||||||||||
| 466 | an interface. This function is always exported unless a routine of the same | ||||||||||||||||||||||||||||
| 467 | name already exists. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =over 4 | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =item test example 1 | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | package Actual; | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | use Mars::Role; | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | package Example; | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | use Mars::Class; | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | test 'Actual'; | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # "Example" | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =back | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =over 4 | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =item test example 2 | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | package Actual; | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | use Mars::Role; | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | sub AUDIT { | ||||||||||||||||||||||||||||
| 498 | die "Example is not an 'actual' thing" if $_[1]->isa('Example'); | ||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | package Example; | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | use Mars::Class; | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | test 'Actual'; | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | # "Example" | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =back | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =cut | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =head2 true | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | true() (Bool) | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | The true function returns a truthy boolean value which is designed to be | ||||||||||||||||||||||||||||
| 518 | practically indistinguishable from the conventional numerical C<1> value. This | ||||||||||||||||||||||||||||
| 519 | function is always exported unless a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =over 4 | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =item true example 1 | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | package Example; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | use Mars::Class; | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | my $true = true; | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | # 1 | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | =back | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | =over 4 | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | =item true example 2 | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | package Example; | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | use Mars::Class; | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | my $false = !true; | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | # 0 | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | =back | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | =cut | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | =head2 with | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | with(Str $name) (Str) | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | The with function registers and consumes roles for the calling package. This | ||||||||||||||||||||||||||||
| 558 | function is an alias of the L</test> function and will perform an | ||||||||||||||||||||||||||||
| 559 | L<"audit"|Mars::Kind/AUDIT> if present. This function is always exported unless | ||||||||||||||||||||||||||||
| 560 | a routine of the same name already exists. | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | =over 4 | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | =item with example 1 | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | package Understanding; | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | use Mars::Role; | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | sub knowledge { | ||||||||||||||||||||||||||||
| 573 | return; | ||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | package Example; | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | use Mars::Class; | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | with 'Understanding'; | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | # "Example" | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =back | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | =over 4 | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =item with example 2 | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | package Understanding; | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | use Mars::Role; | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | sub knowledge { | ||||||||||||||||||||||||||||
| 595 | return; | ||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | sub EXPORT { | ||||||||||||||||||||||||||||
| 599 | return ['knowledge']; | ||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | package Example; | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | use Mars::Class; | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | with 'Understanding'; | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | # "Example" | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =back | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =cut | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | Awncorp, C<awncorp@cpan.org> | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =cut | ||||||||||||||||||||||||||||