Skip to content

Commit f197c1d

Browse files
committed
WIP: Tiny initial hack at some documentation
1 parent badeffa commit f197c1d

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,6 +5299,7 @@ pod/perlboot.pod
52995299
pod/perlbot.pod
53005300
pod/perlcall.pod Perl calling conventions from C
53015301
pod/perlcheat.pod Perl cheat sheet
5302+
pod/perlclass.pod Perl class syntax
53025303
pod/perlclib.pod Internal replacements for standard C library functions
53035304
pod/perlcommunity.pod Perl community information
53045305
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,21 @@ may be outside of the new root.
12781278

12791279
Portability issues: L<perlport/chroot>.
12801280

1281+
=item class NAMESPACE
1282+
1283+
=item class NAMESPACE VERSION
1284+
1285+
=item class NAMESPACE BLOCK
1286+
1287+
=item class NAMESPACE VERSION BLOCK
1288+
1289+
=for Pod::Functions declare a separate global namespace that is an object class
1290+
1291+
Declares the BLOCK or the rest of the compilation unit as being in the given
1292+
namespace, which implements an object class. This behaves similarly to
1293+
L<C<package>/package NAMESPACE>, except that the newly-created package behaves
1294+
as a class.
1295+
12811296
=item close FILEHANDLE
12821297
X<close>
12831298

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

2757+
=item field VARNAME
2758+
X<field>
2759+
2760+
=for Pod::Functions declare a field variable of the current class
2761+
2762+
Declares a new field variable within the current class. Methods and
2763+
L<C<ADJUST>/BLOCK> blocks of the class will have access to this variable as
2764+
if it was a lexical in scope at that point.
2765+
27422766
=item fileno FILEHANDLE
27432767
X<fileno>
27442768

@@ -4303,6 +4327,16 @@ or to force an anon hash constructor use C<+{>:
43034327

43044328
to get a list of anonymous hashes each with only one entry apiece.
43054329

4330+
=item method NAME BLOCK
4331+
X<method>
4332+
4333+
=item method NAME : ATTRS BLOCK
4334+
4335+
=for Pod::Functions declare a method of a class
4336+
4337+
Creates a new named method in the scope of the class that it appears within.
4338+
This is only valid inside a L<C<class>/class NAMESPACE> declaration.
4339+
43064340
=item mkdir FILENAME,MODE
43074341
X<mkdir> X<md> X<directory, create>
43084342

@@ -10561,4 +10595,12 @@ documented in L<perlsyn/"defer blocks">.
1056110595

1056210596
=back
1056310597

10598+
=over
10599+
10600+
=item ADJUST
10601+
10602+
This class-related phaser block is documented in L<perlclass>.
10603+
10604+
=back
10605+
1056410606
=cut

0 commit comments

Comments
 (0)