Class::Plain - a class syntax for the hash-based Perl OO.
Class::Plain
use Class::Plain; class Point { field x : reader; field y : reader; method new : common { my $self = $class->SUPER::new(@_); $self->{x} //= 0; $self->{y} //= 0; return $self; } method move { my ($x, $y) = @_; $self->{x} += $x; $self->{y} += $y; } method to_string { return "($self->{x},$self->{y})"; } } my $point = Point->new(x => 5, y => 10); print $point->x . "\n"; print $point->y . "\n"; print $point->to_string . "\n";
Inheritance:
class Point3D : isa(Point) { field z : reader; method new : common { my $self = $class->SUPER::new(@_); $self->{z} //= 0; return $self; } method move { my ($x, $y, $z) = @_; $self->SUPER::move($x, $y); $self->{z} += $z; } method to_string { return "($self->{x},$self->{y},$self->{z})"; } } my $point3d = Point3D->new(x => 5, y => 10, z => 15); print $point3d->x . "\n"; print $point3d->y . "\n"; print $point3d->z . "\n"; print $point3d->to_string . "\n";
See also Class Plain Cookbook.
This module provides a class syntax for the hash-based Perl OO.
A class is defined by the class keyword.
class
class NAME { ... } class NAME : ATTRS... { ... }
Behaves similarly to the package keyword, but provides a package that defines a new class.
package
As with package, an optional block may be provided. If so, the contents of that block define the new class and the preceding package continues afterwards. If not, it sets the class as the package context of following keywords and definitions.
The following class attributes are supported.
# The single inheritance : isa(SUPER_CLASS) # The multiple inheritance : isa(SUPER_CLASS1) isa(SUPER_CLASS2) # The super class is nothing : isa()
Specifies a supper classes that this class extends.
If the supper class is not specified by isa attribute, the class inherits Class::Plain::Base.
isa
The super class is added to the end of @ISA.
@ISA
If the the super class name doesn't exists in the Perl's symbol table, the super class is loaded.
Otherwise if the super class doesn't have the new method and doesn't have the class names in @ISA, the super class is loaded.
new
: does(ROLE) : does(ROLE1) does(ROLE2)
Specifies roles that this class does. This is the alias for "with" in Role::Tiny.
See also "role".
A field is defined by the field keyword.
field
field NAME; field NAME : ATTR ATTR...;
The following field attributes are supported:
: reader : reader(METHOD_NAME)
Generates a reader method to return the current value of the field. If no name is given, the name of the field is used.
field x : reader; # This is the same as the following code. method x { $self->{x}; }
The different method name can be specified.
field x : reader(x_different_name);
: writer : writer(METHOD_NAME)
Generates a writer method to set a new value of the field from its arguments. If no name is given, the name of the field is used prefixed by set_.
set_
field x : writer; # This is the same as the following code. method set_x { $self->{x} = shift; return $self; }
field x : writer(set_x_different_name);
: rw : rw(METHOD_NAME)
Generates a read-write method to set and get the value of the field. If no name is given, the name of the field is used.
field x : rw; # This is the same as the following code. method x { if (@_) { $self->{x} = shift; return $self; } $self->{x}; }
field x : rw(x_different_name);
method NAME { ... } method NAME : ATTR ATTR ... { ... }
Define a new named method. This behaves similarly to the sub keyword. In addition, the method body will have a lexical called $self which contains the invocant object directly; it will already have been shifted from the @_ array.
sub
$self
@_
Examples:
# An instance method method to_string { my $string = "($self->{x},$self->{y})"; return $string; }
The following method attributes are supported.
: common
Marks that this method is a class-common method, instead of a regular instance method. A class-common method may be invoked on class names instead of instances. Within the method body there is a lexical $class available instead of $self. It will already have been shifted from the @_ array.
$class
# A class method method new : common { my $self = $class->SUPER::new(@_); # ... return $self; }
A role is defined by the role keyword.
role
role NAME { ... } role NAME : ATTRS... { ... }
Class::Plain adopts the role features of Role::Tiny. All features of Role::Tiny can be used.
The examples in the document of Role::Tiny is rewritten to the following codes.
use Class::Plain; role Some::Role { method foo { ... } method bar { ... } } class Some::Class : does(Some::Role) { method foo { ... } # baz is wrapped in the around modifier by Class::Method::Modifiers method baz { ... } }
The same as "does Class Attribute".
In roles, the required method for the composed class can be defined by omitting its method block.
method required_method;
This is alias for "requires" in Role::Tiny.
requires "required_method";
$object->does('Some::Role');
Checks if the object does the role.
Perl 5.16+.
Class::Plain supports the subroutine signatures from Perl 5.26.
Perl 5.26
The subroutine signatures was supported from Perl 5.20, but the parser XS::Parse::Sublike used in Class::Plain can parse only the subroutine signatures after Perl 5.26.
Perl 5.20
use feature 'signatures'; use Class::Plain; Class Point { # ... method move($x = 0, $y = 0) { $self->{x} += $x; $self->{y} += $y; } # ... }
Exmples of Class::Plain.
Class::Plain::Document::Cookbook
The implementation of the Class::Plain module is started from the copy of the source code of Object::Pad.
Class::Plain uses the keywords and attributes that are specified in Corinna.
The keywords: class, field, method.
method
The attributes: isa, reader, writer, common.
reader
writer
common
Only the rw attribute is got from Raku, Moo, Moose.
rw
The class and field keywords are parsed by XS::Parse::Keyword.
The method keyword is parsed by XS::Parse::Sublike.
Role::Tiny is used to implement role features of Class::Plain.
Class::Plain - Github
Yuki Kimoto <kimoto.yuki@gmail.com>
Copyright 2022-2022 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::Plain, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Plain
CPAN shell
perl -MCPAN -e shell install Class::Plain
For more information on module installation, please visit the detailed CPAN module installation guide.