mop minus proposal

I'm creating mop-minus-proposal project on GitHub. Current experimental mop implementation need big core change. so I think writable object orientation support is needed without core change or with minimal core change.


mop-minus-proposal

The following is example. This is not only specification. This code work on Perl 5.20.1+. Important thing is that the following code don't use Perl source filter, such as Filter::Simple.

# Point.pm
package Point {
  use mop::minus;

  has x = 0;
  has y = 0;

  # will be "method clear { ... }"
  sub clear ($self) {
    $self->x(0);
    $self->y(0);
  }
}

1;

# Point3D.pm
package Point3D {
  use mop::minus;

  extends Point;
  with Role1, Role2;

  has z = 0;

  # will be "method clear { ... }"
  sub clear ($self) {
    $self->z(0);
  }
}

1;

# Role1.pm
package Role1 {
  use mop::minus;

  # will be "method foo { ... }"
  sub foo {
    return 'foo';
  }
}

1;

# Role2.pm
package Role1 {
  use mop::minus;

  # will be "method bar { ... }"
  sub bar {
    return 'bar';
  }
}

1;

# main.pl
use Point3D;
my $point = Point3D->new;
print $point->x(0);
my $x = $point->x;

Description: The following code is implemented by keyword API added in Perl 5.12. Old perl can't parse "KEYWORD FOO", but by keyword API feature, Current perl can parse it.

has x = 0;
extends Point;
with Role1, Role1;

 The next is package block syntax. This is added in Perl 5.14. You can write package NAME { ... }.

# Point.pm
package Point {
  ...
}

 The next is subroutine signatures. This is added in Perl 5.20. This is yet experimental. You can write sub ($foo) { ... }. I'm glad method signatures is added in future Perl.

# will be "method clear { ... }"
sub clear ($self) {
  $self->z(0);
}

15 Comments

Thanks for the demo. It shows that introduction of readable OOP in Perl is less a technical problem but a problem of agreement. We could already have this if it was put into core, enabled by default (!) and not marked as experimental (!!). Only when it's enough to put a single use v5.2X; at the top of your code to, I will start telling people that Perl natively supports OOP (since version 5.2X).

mop::minus looks like more like Moo/Mouse/Mo than MOP.

MOP means « Meta Object Protocol ». I see nothing like that in mop::minus. mop::minus seems to be just some cool syntax for Object::Tiny. This is just deceptively simple.

Besides what Olivier mentioned above I wonder about the use of Parse::Keyword. The author of that module states in the POD for that module that it is deprecated and should not be used.

The more of yet another /^(mop|Mo)/ module or yet another comment like this I read, the more I think about switching my favorite programming language. A clean, default syntax for defining classes and methods in Perl (as usual in other programming languages with support of OOP) is almost as vaporware as Perl 6. Sure one can discuss about details and differences between all this Moooos and mops, but without decision on any standard way to do OOP in Perl, the language is going nowhere. I am tired of thinking about which way modern Perl may or may not go, so my next project will definitely not be in Perl anymore.

Parse::Keyword has issues, but it is possible to work around them. (And I don't think Yuki Kimoto's code triggers the problems in Parse::Keyword anyway.)

There are a lot of class/role frameworks, but the decision making process for which to use is actually very simple:

Start with Moo, and if you outgrow it, use Moose.

You can mostly forget about the others.

Also there's Moops which is not a class/role framework itself but syntactic sugar for either Moo or Moose.

Yuki,

You might be interested to know that I am now working on YAMP (Yet Another Mop Prototype) (I really should do something to link all the prototypes, perhaps merge up all the separate git repos into a single repo with branches for each prototype, hmm, anyway, that is for later). At the moment it is in the VERY VERY VERY VERY VERY VERY early stages, so please be careful to not infer too much about what the final version might look like or how it might behave.

As for mop::minus, I totally agree with you, the previous (no longer current) prototype (and the one before that) was too complicated and requires to many things to be added to the Perl core. In this latest prototype I am looking to add the absolute minimum amount of code, behavior and syntax possible to the Perl core while still providing a powerful MOP when needed. I am also trying keep my objectivity as much as possible. My own opinions tend toward the be "strict, verbose & theoretically correct" side, but a core MOP should not enforce that opinion (perhaps the optional syntax layer will, but that is still to be determined).

Yours, Stevan

While I appreciate the effort, I cannot understand why another OOP support when there already a lot, and with a very good implementation (Moose being probably the most adopted).

Leave a comment

About Yuki Kimoto

user-pic I'm Yuki Kimoto, Japanese Perl programmer.I create Perl modules and applications. GitPrep, SPVM, DBIx::Custom, Validator::Custom, Test::Mojo, Mojolicious::Plugin::AutoRoute, etc.