Skip to content

First round of feature 'class' #20647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial attack at pod/perlclass.pod
  • Loading branch information
leonerd committed Feb 10, 2023
commit fd11211b37c20262922903f6435c1c09ac046e5c
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -5354,6 +5354,7 @@ pod/perlboot.pod
pod/perlbot.pod
pod/perlcall.pod Perl calling conventions from C
pod/perlcheat.pod Perl cheat sheet
pod/perlclass.pod Perl class syntax
pod/perlclib.pod Internal replacements for standard C library functions
pod/perlcommunity.pod Perl community information
pod/perldata.pod Perl data structures
Expand Down
9 changes: 5 additions & 4 deletions ext/Pod-Functions/t/Functions.t
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ Functions for filehandles, files, or directories:
Keywords related to the control flow of your Perl program:
__FILE__, __LINE__, __PACKAGE__, __SUB__, break, caller,
continue, die, do, dump, eval, evalbytes, exit, goto,
last, next, redo, return, sub, wantarray
last, method, next, redo, return, sub, wantarray

Keywords related to scoping:
caller, import, local, my, our, package, state, use
caller, class, field, import, local, my, our, package,
state, use

Miscellaneous functions:
defined, formline, lock, prototype, reset, scalar, undef
Expand All @@ -132,8 +133,8 @@ Keywords related to Perl modules:
do, import, no, package, require, use

Keywords related to classes and object-orientation:
bless, dbmclose, dbmopen, package, ref, tie, tied, untie,
use
bless, class, dbmclose, dbmopen, field, method, package,
ref, tie, tied, untie, use

Low-level socket functions:
accept, bind, connect, getpeername, getsockname,
Expand Down
1 change: 1 addition & 0 deletions pod/perl.pod
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ aux h2ph h2xs perlbug pl2pm pod2html pod2man splain xsubpp
perlform Perl formats
perlobj Perl objects
perltie Perl objects hidden behind simple variables
perlclass Perl class syntax
perldbmfilter Perl DBM filters

perlipc Perl interprocess communication
Expand Down
60 changes: 60 additions & 0 deletions pod/perlclass.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
=head1 NAME

perlclass - Perl class syntax

=head1 DESCRIPTION

use v5.36;
use feature 'class';

class My::Example 1.234 {
field $x;
ADJUST { $x = "Hello, world"; }

method print_message { say $x; }
}

My::Example->new->print_message;

C<class> is like C<package>. Classes automatically get a C<new> method; you
don't have to (and should not) write one.

C<method> is like C<sub> but automatically gains a C<$self> lexical.

C<ADJUST> blocks run during construction and are the way to add code that runs
during the construction time of each instance. They also have a C<$self>
lexical.

C<field> is like C<my> but only visible within C<methods> and C<ADJUST>
blocks.

Instances get their own value storage for fields

class My::Counter {
field $count; ADJUST { $count = 0; }

method incr { $count++ }
method val { return $count; }
}

my $ca = My::Counter->new;
$ca->incr; $ca->incr; $ca->incr;

my $cb = My::Counter->new;
$cb->incr;

say "Counter A is at ", $ca->val;
say "Counter B is at ", $cb->val;

C<methods> always act as if C<use feature 'signatures'> is in effect. You do
not need to worry about the C<$self> lexical: it is automatically created and
populated with the object instance, which will not appear in the arguments
list as far as the signature is concerned.

class Example::WithSignatures {
method greet($name = "someone") {
say "Hello, $name";
}
}

=cut
71 changes: 64 additions & 7 deletions pod/perlfunc.pod
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ L<C<dump>|/dump LABEL>, L<C<eval>|/eval EXPR>,
L<C<evalbytes>|/evalbytes EXPR>, L<C<exit>|/exit EXPR>,
L<C<__FILE__>|/__FILE__>, L<C<goto>|/goto LABEL>,
L<C<last>|/last LABEL>, L<C<__LINE__>|/__LINE__>,
L<C<method>|/method NAME BLOCK>,
L<C<next>|/next LABEL>, L<C<__PACKAGE__>|/__PACKAGE__>,
L<C<redo>|/redo LABEL>, L<C<return>|/return EXPR>,
L<C<sub>|/sub NAME BLOCK>, L<C<__SUB__>|/__SUB__>,
Expand Down Expand Up @@ -293,9 +294,15 @@ current scope.

=for Pod::Functions =Namespace

L<C<caller>|/caller EXPR>, L<C<import>|/import LIST>,
L<C<local>|/local EXPR>, L<C<my>|/my VARLIST>, L<C<our>|/our VARLIST>,
L<C<package>|/package NAMESPACE>, L<C<state>|/state VARLIST>,
L<C<caller>|/caller EXPR>,
L<C<class>|/class NAMESPACE>,
L<C<field>|/field VARNAME>,
L<C<import>|/import LIST>,
L<C<local>|/local EXPR>,
L<C<my>|/my VARLIST>,
L<C<our>|/our VARLIST>,
L<C<package>|/package NAMESPACE>,
L<C<state>|/state VARLIST>,
L<C<use>|/use Module VERSION LIST>

L<C<state>|/state VARLIST> is available only if the
Expand Down Expand Up @@ -343,11 +350,18 @@ X<object> X<class> X<package>

=for Pod::Functions =Objects

L<C<bless>|/bless REF,CLASSNAME>, L<C<dbmclose>|/dbmclose HASH>,
L<C<bless>|/bless REF,CLASSNAME>,
L<C<class>|/class NAMESPACE>,
L<C<dbmclose>|/dbmclose HASH>,
L<C<dbmopen>|/dbmopen HASH,DBNAME,MASK>,
L<C<package>|/package NAMESPACE>, L<C<ref>|/ref EXPR>,
L<C<tie>|/tie VARIABLE,CLASSNAME,LIST>, L<C<tied>|/tied VARIABLE>,
L<C<untie>|/untie VARIABLE>, L<C<use>|/use Module VERSION LIST>
L<C<field>|/field VARNAME>,
L<C<method>|/method NAME BLOCK>,
L<C<package>|/package NAMESPACE>,
L<C<ref>|/ref EXPR>,
L<C<tie>|/tie VARIABLE,CLASSNAME,LIST>,
L<C<tied>|/tied VARIABLE>,
L<C<untie>|/untie VARIABLE>,
L<C<use>|/use Module VERSION LIST>

=item Low-level socket functions
X<socket> X<sock>
Expand Down Expand Up @@ -426,6 +440,7 @@ L<C<time>|/time>, L<C<times>|/times>

=for Pod::Functions =!Non-functions

C<ADJUST>,
C<and>,
C<AUTOLOAD>,
C<BEGIN>,
Expand Down Expand Up @@ -1317,6 +1332,21 @@ may be outside of the new root.

Portability issues: L<perlport/chroot>.

=item class NAMESPACE

=item class NAMESPACE VERSION

=item class NAMESPACE BLOCK

=item class NAMESPACE VERSION BLOCK

=for Pod::Functions declare a separate global namespace that is an object class

Declares the BLOCK or the rest of the compilation unit as being in the given
namespace, which implements an object class. This behaves similarly to
L<C<package>|/package NAMESPACE>, except that the newly-created package behaves
as a class.

=item close FILEHANDLE
X<close>

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

=item field VARNAME
X<field>

=for Pod::Functions declare a field variable of the current class

Declares a new field variable within the current class. Methods and
C<ADJUST> blocks of the class will have access to this variable as if it
was a lexical in scope at that point.

=item fileno FILEHANDLE
X<fileno>

Expand Down Expand Up @@ -4352,6 +4391,16 @@ or to force an anon hash constructor use C<+{>:

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

=item method NAME BLOCK
X<method>

=item method NAME : ATTRS BLOCK

=for Pod::Functions declare a method of a class

Creates a new named method in the scope of the class that it appears within.
This is only valid inside a L<C<class>|/class NAMESPACE> declaration.

=item mkdir FILENAME,MODE
X<mkdir> X<md> X<directory, create>

Expand Down Expand Up @@ -10714,4 +10763,12 @@ documented in L<perlsyn/"defer blocks">.

=back

=over

=item ADJUST

This class-related phaser block is documented in L<perlclass>.

=back

=cut
4 changes: 4 additions & 0 deletions win32/pod.mak
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ POD = perl.pod \
perlbot.pod \
perlcall.pod \
perlcheat.pod \
perlclass.pod \
perlclib.pod \
perlcommunity.pod \
perldata.pod \
Expand Down Expand Up @@ -272,6 +273,7 @@ MAN = perl.man \
perlbot.man \
perlcall.man \
perlcheat.man \
perlclass.man \
perlclib.man \
perlcommunity.man \
perldata.man \
Expand Down Expand Up @@ -447,6 +449,7 @@ HTML = perl.html \
perlbot.html \
perlcall.html \
perlcheat.html \
perlclass.html \
perlclib.html \
perlcommunity.html \
perldata.html \
Expand Down Expand Up @@ -622,6 +625,7 @@ TEX = perl.tex \
perlbot.tex \
perlcall.tex \
perlcheat.tex \
perlclass.tex \
perlclib.tex \
perlcommunity.tex \
perldata.tex \
Expand Down