@@ -9,9 +9,14 @@ perlclass - Perl class syntax reference
99
1010 class My::Example 1.234 {
1111 field $x;
12- ADJUST { $x = "Hello, world"; }
1312
14- method print_message { say $x; }
13+ ADJUST {
14+ $x = "Hello, world";
15+ }
16+
17+ method print_message {
18+ say $x;
19+ }
1520 }
1621
1722 My::Example->new->print_message;
@@ -40,59 +45,90 @@ the scope of current package:
4045
4146=head2 class
4247
43- class NAME BLOCK
48+ class NAME BLOCK
4449
45- class NAME VERSION BLOCK
50+ class NAME VERSION BLOCK
4651
4752The C<class> keyword declares a new package which is intended to be a class.
4853All other keywords from the C<class> feature should be used in scope of this
4954declaration.
5055
51- There are following differences between C<package> and C<class> keywords:
52-
53- =over
54-
55- =item * Creates a new scope for variables declared using the L<field> keyword
56-
57- =item * Classes automatically get a constructor named C<new>
58-
59- You don't have to (and should not) write one.
56+ class WithVersion 1.000 {
57+ # class definition goes here
58+ }
6059
61- =back
60+ C<class> and C<package> declarations are similar, but classes automatically get
61+ a constructor named C<new> - You don't have to (and should not) write one.
62+ Additionally, in the class BLOCK you are allowed to declare fields and methods.
6263
6364=head2 field
6465
65- field VARIABLE_NAME;
66+ field VARIABLE_NAME;
6667
6768Fields are variables which are visible in the scope of the class - more
6869specifically within L<method> and L<ADJUST> blocks. Each class instance get
6970their own storage of fields, independent of each other.
7071
7172A field behaves like a normal lexically scoped variable. It has a sigil and is
72- private to the class unless an accessor method is created. The main difference
73- is that different instances access different values in the same scope.
73+ private to the class (though creation of an accessor method will make it
74+ accessible from the outside). The main difference is that different instances
75+ access different values in the same scope.
76+
77+ class WithFields {
78+ field $scalar;
79+ field @array;
80+ field %hash;
81+
82+ ADJUST {
83+ $scalar = 42;
84+ @array = qw(this is just an array);
85+ %hash = (species => 'Marsian', planet => 'Mars');
86+ }
87+ }
7488
7589=head2 method
7690
77- method METHOD_NAME SIGNATURE BLOCK
91+ method METHOD_NAME SIGNATURE BLOCK
92+ method METHOD_NAME BLOCK
93+ method SIGNATURE BLOCK
94+ method BLOCK
7895
79- Methods are subroutines intended to call in the context of class objects.
96+ Methods are subroutines intended to be called in the context of class objects.
8097
8198A variable named C<$self> populated with the current object instance will automatically be
8299created in the lexical scope of C<method>.
83100
84101Methods always act as if C<use feature 'signatures'> is in effect, but C<$self>
85102will not appear in the arguments list as far as the signature is concerned.
86103
87- class Class::WithSignatures {
104+ class WithMethods {
105+ field $greetings;
106+
107+ ADJUST {
108+ $greetings = "Hello";
109+ }
110+
88111 method greet($name = "someone") {
89- say "Hello, $name";
112+ say "$greetings, $name";
113+ }
114+ }
115+
116+ Just like regular subroutines, methods I<can> be anonymous:
117+
118+ class AnonMethodFactory {
119+
120+ method get_anon_method {
121+ return method {
122+ return 'this is an anonymous method';
123+ };
90124 }
91125 }
92126
93127=head1 ATTRIBUTES
94128
95- Specific aspects of the keywords defined above are declared using I<attributes>.
129+ Specific aspects of the keywords mentioned above are managed using
130+ I<attributes>. Attributes all start with a colon, and one or more of them can
131+ be appended after the item's name, separated by a space.
96132
97133=head2 Class attributes
98134
@@ -101,9 +137,9 @@ Specific aspects of the keywords defined above are declared using I<attributes>.
101137Classes may inherit from B<one> superclass, by using the C<:isa> class
102138attribute.
103139
104- class Example::Base { ... }
140+ class Example::Base { ... }
105141
106- class Example::Subclass :isa(Example::Base) { ... }
142+ class Example::Subclass :isa(Example::Base) { ... }
107143
108144Inherited methods are visible and may be invoked. Fields are always lexical
109145and therefore not visible by inheritence.
@@ -112,18 +148,18 @@ The C<:isa> attribute may request a minimum version of the base class; it is
112148applied similar to C<use> - if the provided version is too low it will fail at
113149compile time.
114150
115- class Example::Subclass :isa(Example::Base 2.345) { ... }
151+ class Example::Subclass :isa(Example::Base 2.345) { ... }
116152
117153The C<:isa> attribute will attempt to C<require> the named module if it is not
118154already loaded.
119155
120156=head2 Field attributes
121157
122- TODO
158+ None yet.
123159
124160=head2 Method attributes
125161
126- TODO
162+ None yet.
127163
128164=head1 OBJECT LIFECYCLE
129165
@@ -132,29 +168,40 @@ TODO
132168Each object begins its life with a constructor call. The constructor is always
133169named C<new> and is invoked like a method call on the class name:
134170
135- my $object = My::Class->new(%arguments);
171+ my $object = My::Class->new(%arguments);
136172
137173During the construction, class fields are compared to C<%arguments> hash and
138174populated where possible.
139175
140176=head2 Adjustment
141177
142178Object adjustment can be performed during the construction to run user-defined
143- code. It is done with the help of C<ADJUST> blocks, which run during the
144- construction time of each instance .
179+ code. It is done with the help of C<ADJUST> blocks, which are called in order
180+ of declaration .
145181
146- They are similar to C<BEGIN> blocks, which
147- run during the compilation of a package. However, they also have access to
148- C<$self> lexical ( object instance) .
182+ They are similar to C<BEGIN> blocks, which run during the compilation of a
183+ package. However, they also have access to C<$self> lexical (object instance)
184+ and all object fields created up to that point .
149185
150186=head2 Lifetime
151187
152- After the construction phase, object is ready to be used. Just like a normal
153- reference, it will live as long as its reference count is greater than zero.
188+ After the construction phase, object is ready to be used.
189+
190+ Using C<blessed> (C<Scalar::Util::blessed> or C<builtin::blessed>) on the
191+ object will return the name of the class, while C<reftype>
192+ (C<Scalar::Util::reftype> or C<builtin::reftype>) will return the string
193+ C<'OBJECT'>.
154194
155195=head2 Destruction
156196
157- TODO
197+ Just like with other references, when object reference count reaches zero it
198+ will automatically be destroyed.
199+
200+ =head1 AUTHORS
201+
202+ Paul Evans
203+
204+ Bartosz Jarzyna
158205
159206=cut
160207
0 commit comments