@@ -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,93 @@ 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+ 
93+     method METHOD_NAME BLOCK
94+ 
95+     method SIGNATURE BLOCK
96+ 
97+     method BLOCK
7898
79- Methods are subroutines intended to call  in the context of class objects.
99+ Methods are subroutines intended to be called  in the context of class objects.
80100
81101A variable named C<$self> populated with the current object instance will automatically be
82102created in the lexical scope of C<method>.
83103
84104Methods always act as if C<use feature 'signatures'> is in effect, but C<$self>
85105will not appear in the arguments list as far as the signature is concerned.
86106
87-     class Class::WithSignatures {
107+     class WithMethods {
108+         field $greetings;
109+ 
110+         ADJUST {
111+             $greetings = "Hello";
112+         }
113+ 
88114        method greet($name = "someone") {
89-             say "Hello, $name";
115+             say "$greetings, $name";
116+         }
117+     }
118+ 
119+ Just like regular subroutines, methods I<can> be anonymous:
120+ 
121+     class AnonMethodFactory {
122+ 
123+         method get_anon_method {
124+             return method {
125+                 return 'this is an anonymous method';
126+             };
90127        }
91128    }
92129
93130=head1 ATTRIBUTES
94131
95- Specific aspects of the keywords defined above are declared using I<attributes>.
132+ Specific aspects of the keywords mentioned above are managed using
133+ I<attributes>. Attributes all start with a colon, and one or more of them can
134+ be appended after the item's name, separated by a space.
96135
97136=head2 Class attributes
98137
@@ -101,9 +140,9 @@ Specific aspects of the keywords defined above are declared using I<attributes>.
101140Classes may inherit from B<one> superclass, by using the C<:isa> class
102141attribute.
103142
104-   class Example::Base { ... }
143+      class Example::Base { ... }
105144
106-   class Example::Subclass :isa(Example::Base) { ... }
145+      class Example::Subclass :isa(Example::Base) { ... }
107146
108147Inherited methods are visible and may be invoked. Fields are always lexical
109148and therefore not visible by inheritence.
@@ -112,18 +151,18 @@ The C<:isa> attribute may request a minimum version of the base class; it is
112151applied similar to C<use> - if the provided version is too low it will fail at
113152compile time.
114153
115-   class Example::Subclass :isa(Example::Base 2.345) { ... }
154+      class Example::Subclass :isa(Example::Base 2.345) { ... }
116155
117156The C<:isa> attribute will attempt to C<require> the named module if it is not
118157already loaded.
119158
120159=head2 Field attributes
121160
122- TODO 
161+ None yet. 
123162
124163=head2 Method attributes
125164
126- TODO 
165+ None yet. 
127166
128167=head1 OBJECT LIFECYCLE
129168
@@ -132,29 +171,40 @@ TODO
132171Each object begins its life with a constructor call. The constructor is always
133172named C<new> and is invoked like a method call on the class name:
134173
135- 	 my $object = My::Class->new(%arguments);
174+      my $object = My::Class->new(%arguments);
136175
137176During the construction, class fields are compared to C<%arguments> hash and
138177populated where possible.
139178
140179=head2 Adjustment
141180
142181Object 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 .
182+ code. It is done with the help of C<ADJUST> blocks, which are called in order 
183+ of declaration .
145184
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) .
185+ They are similar to C<BEGIN> blocks, which run during the compilation of a 
186+ package. However, they also have access to C<$self> lexical (object instance) 
187+ and all  object fields created up to that point .
149188
150189=head2 Lifetime
151190
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.
191+ After the construction phase, object is ready to be used.
192+ 
193+ Using C<blessed> (C<Scalar::Util::blessed> or C<builtin::blessed>) on the
194+ object will return the name of the class, while C<reftype>
195+ (C<Scalar::Util::reftype> or C<builtin::reftype>) will return the string
196+ C<'OBJECT'>.
154197
155198=head2 Destruction
156199
157- TODO
200+ Just like with other references, when object reference count reaches zero it
201+ will automatically be destroyed.
202+ 
203+ =head1 AUTHORS
204+ 
205+ Paul Evans
206+ 
207+ Bartosz Jarzyna
158208
159209=cut
160210
0 commit comments