Skip to content

Commit 3da6f99

Browse files
committed
WIP: Tiny initial hack at some documentation
1 parent 30cf3e6 commit 3da6f99

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5298,6 +5298,7 @@ pod/perlboot.pod
52985298
pod/perlbot.pod
52995299
pod/perlcall.pod Perl calling conventions from C
53005300
pod/perlcheat.pod Perl cheat sheet
5301+
pod/perlclass.pod Perl class syntax
53015302
pod/perlclib.pod Internal replacements for standard C library functions
53025303
pod/perlcommunity.pod Perl community information
53035304
pod/perldata.pod Perl data structures

pod/perlclass.pod

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
=head1 NAME
2+
3+
perlclass - Perl class syntax
4+
5+
=head1 DESCRIPTION
6+
7+
use v5.36;
8+
use feature 'class';
9+
10+
class My::Example 1.234 {
11+
field $x;
12+
ADJUST { $x = "Hello, world"; }
13+
14+
method print_message { say $x; }
15+
}
16+
17+
My::Example->new->print_message;
18+
19+
C<class> is like C<package>. Classes automatically get a C<new> method; you
20+
don't have to (and should not) write one.
21+
22+
C<method> is like C<sub> but automatically gains a C<$self> lexical.
23+
24+
C<ADJUST> blocks run during construction and are the way to add code that runs
25+
during the construction time of each instance. They also have a C<$self>
26+
lexical.
27+
28+
C<field> is like C<my> but only visible within C<methods> and C<ADJUST>
29+
blocks.
30+
31+
Instances get their own value storage for fields
32+
33+
class My::Counter {
34+
field $count; ADJUST { $count = 0; }
35+
36+
method incr { $count++ }
37+
method val { return $count; }
38+
}
39+
40+
my $ca = My::Counter->new;
41+
$ca->incr; $ca->incr; $ca->incr;
42+
43+
my $cb = My::Counter->new;
44+
$cb->incr;
45+
46+
say "Counter A is at ", $ca->val;
47+
say "Counter B is at ", $cb->val;
48+
49+
C<methods> always act as if C<use feature 'signatures'> is in effect. You do
50+
not need to worry about the C<$self> lexical: it is automatically created and
51+
populated with the object instance, which will not appear in the arguments
52+
list as far as the signature is concerned.
53+
54+
class Example::WithSignatures {
55+
method greet($name = "someone") {
56+
say "Hello, $name";
57+
}
58+
}
59+
60+
=cut

pod/perlfunc.pod

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ L<C<last>|/last LABEL>, L<C<__LINE__>|/__LINE__>,
265265
L<C<next>|/next LABEL>, L<C<__PACKAGE__>|/__PACKAGE__>,
266266
L<C<redo>|/redo LABEL>, L<C<return>|/return EXPR>,
267267
L<C<sub>|/sub NAME BLOCK>, L<C<__SUB__>|/__SUB__>,
268+
L<C<method>|/method NAME BLOCK>,
268269
L<C<wantarray>|/wantarray>
269270

270271
L<C<break>|/break> is available only if you enable the experimental
@@ -295,7 +296,8 @@ current scope.
295296

296297
L<C<caller>|/caller EXPR>, L<C<import>|/import LIST>,
297298
L<C<local>|/local EXPR>, L<C<my>|/my VARLIST>, L<C<our>|/our VARLIST>,
298-
L<C<package>|/package NAMESPACE>, L<C<state>|/state VARLIST>,
299+
L<C<package>|/package NAMESPACE>, L<C<class>|/class NAMESPACE>,
300+
L<C<field>|/field VARNAME>, L<C<state>|/state VARLIST>,
299301
L<C<use>|/use Module VERSION LIST>
300302

301303
L<C<state>|/state VARLIST> is available only if the
@@ -345,7 +347,9 @@ X<object> X<class> X<package>
345347

346348
L<C<bless>|/bless REF,CLASSNAME>, L<C<dbmclose>|/dbmclose HASH>,
347349
L<C<dbmopen>|/dbmopen HASH,DBNAME,MASK>,
348-
L<C<package>|/package NAMESPACE>, L<C<ref>|/ref EXPR>,
350+
L<C<package>|/package NAMESPACE>, L<C<class>|/class NAMESPACE>,
351+
L<C<field>|/field VARNAME>, L<C<method>|/method NAME BLOCK>,
352+
L<C<ref>|/ref EXPR>,
349353
L<C<tie>|/tie VARIABLE,CLASSNAME,LIST>, L<C<tied>|/tied VARIABLE>,
350354
L<C<untie>|/untie VARIABLE>, L<C<use>|/use Module VERSION LIST>
351355

@@ -427,6 +431,7 @@ L<C<time>|/time>, L<C<times>|/times>
427431
=for Pod::Functions =!Non-functions
428432

429433
C<and>,
434+
C<ADJUST>,
430435
C<AUTOLOAD>,
431436
C<BEGIN>,
432437
C<catch>,
@@ -1278,6 +1283,21 @@ may be outside of the new root.
12781283

12791284
Portability issues: L<perlport/chroot>.
12801285

1286+
=item class NAMESPACE
1287+
1288+
=item class NAMESPACE VERSION
1289+
1290+
=item class NAMESPACE BLOCK
1291+
1292+
=item class NAMESPACE VERSION BLOCK
1293+
1294+
=for Pod::Functions declare a separate global namespace that is an object class
1295+
1296+
Declares the BLOCK or the rest of the compilation unit as being in the given
1297+
namespace, which implements an object class. This behaves similarly to
1298+
L<C<package>/package NAMESPACE>, except that the newly-created package behaves
1299+
as a class.
1300+
12811301
=item close FILEHANDLE
12821302
X<close>
12831303

@@ -2749,6 +2769,15 @@ A special token that returns the name of the file in which it occurs.
27492769
It can be altered by the mechanism described at
27502770
L<perlsyn/"Plain Old Comments (Not!)">.
27512771

2772+
=item field VARNAME
2773+
X<field>
2774+
2775+
=for Pod::Functions declare a field variable of the current class
2776+
2777+
Declares a new field variable within the current class. Methods and
2778+
L<C<ADJUST>/BLOCK> blocks of the class will have access to this variable as
2779+
if it was a lexical in scope at that point.
2780+
27522781
=item fileno FILEHANDLE
27532782
X<fileno>
27542783

@@ -4313,6 +4342,16 @@ or to force an anon hash constructor use C<+{>:
43134342

43144343
to get a list of anonymous hashes each with only one entry apiece.
43154344

4345+
=item method NAME BLOCK
4346+
X<method>
4347+
4348+
=item method NAME : ATTRS BLOCK
4349+
4350+
=for Pod::Functions declare a method of a class
4351+
4352+
Creates a new named method in the scope of the class that it appears within.
4353+
This is only valid inside a L<C<class>/class NAMESPACE> declaration.
4354+
43164355
=item mkdir FILENAME,MODE
43174356
X<mkdir> X<md> X<directory, create>
43184357

@@ -10571,4 +10610,12 @@ documented in L<perlsyn/"defer blocks">.
1057110610

1057210611
=back
1057310612

10613+
=over
10614+
10615+
=item ADJUST
10616+
10617+
This class-related phaser block is documented in L<perlclass>.
10618+
10619+
=back
10620+
1057410621
=cut

0 commit comments

Comments
 (0)