1. Perl
  2. reading material

Let's review hash - based object orientation

I personally like hash-based object orientation. mop feels too complicated to do simple things. If it takes years or years depending on when or when you can put mop in the core, put one hash-based object-oriented module in the core first. If this can be done, the convenience of Perl users will be greatly improved.

I don't expect much from mop myself. Because hash-based object-oriented can create more than enough classes. Hash-based object-oriented programming is simple, but it is possible to create a class that is functionally necessary and sufficient.

We will write about the advantages of hash-based object-oriented programming.

Constructor performance

Hash-based object-oriented programming has much better performance in constructor generation.

my $obj = SomeClass->new;

This is because the received value is simply stored in a hash and blessed. You might think of performance, but when it comes to large frameworks, the constructor generation process uses an extremely large number of times, and it is sometimes slowed down here.

If you're worried about that, I think it's better to write with hash-based object thinking from the beginning and have faster performance.

Accessor performance

Accessors are often called. What if there is a module performance bottleneck in the part that calls a lot of accessors? Even in such a case, hash-based object-oriented programming can easily solve the problem.

That's because all you have to do is change the accessor call to access to the hash. This will be 20 to 30 times faster.

# Method call

# Hash access

If you were writing in mop, you wouldn't be able to do this.

Simple is Best

You can implement a large, nicely implemented framework with a simple object-oriented module without a lot of features. If you look at a framework like Mojolicious, you'll see that it's a concise object-oriented module that creates well-organized classes.

package SomeClass;
use Mojo::Base -base;

has x => 0;
has y => 0;

It's usually enough to generate accessors and set default values. If it is complicated, it will be a heavy burden.

It's easy to use single inheritance and delegation for class design

When designing a class, I find it best to use the techniques of single inheritance and delegation. Designing a class with this policy makes the class easier to understand. There is no need to use multiple inheritance, and I think it tends to complicate the structure of the class.

For example, if a single inheritance doesn't make it in time, use delegation. Suppose you want to inherit R and add the functionality of class Q to class P. In such a case, create object Q and give it to object P. This is a technique called delegation.

package P;
use Mojo::Base 'R';

use Q;

has q => sub {Q->new};

By doing this, you can create class P that can use the functions of class Q. Of course, since it inherits a single R, the functions of R can be used directly from P.

Intuitive override

For example, suppose you want to override a constructor in a subclass. Hash-based object-oriented programming is intuitive and familiar to other languages.

sub new {
  my $self = shift->SUERP::new(@_);

  # Gonyo Gonyo

  return $self;

BUILD and BUILDARGS of mop are difficult to understand.

The destructor is DESTROY

The destructor is DESTROY as per Perl's custom. You don't have to bother to remember mop's DEMOLISH.

Hash - based object - oriented modules are recommended

I recommend hash-based object-oriented over meta-object protocol-based object-oriented. I also want the core to contain one hash-based object-oriented module.

The meta-object protocol-based object-orientation was once crazy and thought to be awesome, but I don't agree with it.

Related Informatrion