1Mojo::Base(3) User Contributed Perl Documentation Mojo::Base(3)
2
3
4
6 Mojo::Base - Minimal Base Class For Mojo Projects
7
9 package Car;
10 use base 'Mojo::Base';
11
12 __PACKAGE__->attr('driver');
13 __PACKAGE__->attr('doors' => 2);
14 __PACKAGE__->attr([qw/passengers seats/] => sub { 2 });
15
16 package main;
17
18 my $bmw = Car->new;
19 print $bmw->doors;
20 print $bmw->doors(5)->doors;
21
22 my $mercedes = Car->new(driver => 'Sebastian');
23 print $mercedes->passengers(7)->passengers;
24
26 Mojo::Base is a simple base class for Mojo projects.
27
29 "new"
30 my $instance = BaseSubClass->new;
31 my $instance = BaseSubClass->new(name => 'value');
32 my $instance = BaseSubClass->new({name => 'value'});
33
34 This base class provides a basic object constructor. You can pass it
35 either a hash or a hash reference with attribute values.
36
37 "attr"
38 __PACKAGE__->attr('name');
39 __PACKAGE__->attr([qw/name1 name2 name3/]);
40 __PACKAGE__->attr(name => 'foo');
41 __PACKAGE__->attr(name => sub { ... });
42 __PACKAGE__->attr([qw/name1 name2 name3/] => 'foo');
43 __PACKAGE__->attr([qw/name1 name2 name3/] => sub { ... });
44
45 Create attributes. An arrayref can be used to create more than one
46 attribute. Pass an optional second argument to set a default value, it
47 should be a constant or a sub reference. The sub reference will be
48 excuted at accessor read time if there's no set value.
49
51 Mojolicious, Mojolicious::Guides, <http://mojolicious.org>.
52
53
54
55perl v5.12.3 2010-08-12 Mojo::Base(3)