Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 22, 2022 10:56 pm GMT

Class::Plain - Class Syntax for Hash-Based Perl OO

Class::Plain was released at 2022-09-22.

use Class::Plain;class Point {  field x;  field y;  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 describe {    print "A point at ($self->{x}, $self->{y})
"; }}my $point = Point->new(x => 5, y => 10);$point->describe;

Inheritance:

class Point3D : isa(Point) {  field z;  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 describe {    print "A point at ($self->{x}, $self->{y}, $self->{z})
"; }}

I'm strongly interested in Perl OO. I study Corinna and Object::Pad now.

A few weaks ago, I wondered "These class syntax can be applied to the traditional Perl hash-based OO modules. The syntax can be used with the all existing modules".

I remembered many OO modules. Net::FTP, IO::Socket::INET, IO::Socket::IP, LWP::UserAgent, HTTP::Tiny, XML::Simple, Data::Page, CGI, Digest::MD5, Digest::SHA, Mojolicious, Catalyst, DBIx::Class etc .

Class::Plain - Class Syntax for Hash-Based Perl OO


Original Link: https://dev.to/yukikimoto/classplain-class-syntax-for-hash-based-perl-oo-4j6a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To