DEV Community

Yuki Kimoto
Yuki Kimoto

Posted on

SPVM now supports object-oriented programming in Perl

SPVM now supports object-oriented programming in Perl.

One of the goals of SPVM is to realize object-oriented programming in Perl as a statically-typed language.

To achieve this, it was necessary to understand object-oriented programming in Perl properly, and then to see how it could be realized in a statically-typed language, through a number of implementations.

The first challenge is that no one knows if it can be done, because no programming language has ever realized Perl's object-oriented programming in a statically-typed language.

It is a task that we do not even know if we can do.

We were getting close to Perl's object-oriented way of doing things, but there was a lot of trial and error and backward-compatibility breaking.

Last Saturday and Sunday, I came up with the final piece that would make SPVM object-oriented in Perl. And that is what I implemented this Monday. Then I applied it to SPVM::File::Spec and SPVM::IO.

Please read the source code and you will see that SPVM's object orientation can be described in exactly the same way as Perl's object orientation.

Overview of SPVM's object orientation

To understand SPVM's object orientation, Point3D and Point are good examples. Class::Accessor(::Fast), Moose/Moo-based implementations, Mojo::Base implementations, and implementations using bless. My personal feeling is that 90% of the time, these are the easiest to port to SPVM.

class Point {
  # Interfaces
  interface Stringable;
  interface Cloneable;

  # Fields
  has x : rw protected int;
  has y : rw protected int;

  # Class methods
  static method new : Point ($x = 0 : int, $y = 0 : int) {
    my $self = new Point;

    $self->init($x, $y);

    return $self;
  }

  # Instance methods
  protected method init : Point ($x = 0 : int, $y = 0 : int) {
    $self->{x} = $x;
    $self->{y} = $y;
  }

  method clear : void () {
    $self->{x} = 0;
    $self->{y} = 0;
  }

  method clone : Point () {
    my $self_clone = Point->new($self->x, $self->y);

    return $self_clone;
  }

  method to_string : string () {
    my $x = $self->x;
    my $y = $self->y;

    my $string = "($x,$y)";

    return $string;
  }
}

class Point3D extends Point {

  # Fields
  has z : rw protected int;

  # Class method
  static method new : Point3D ($x = 0 : int, $y = 0 : int, $z = 0 : int) {
    my $self = new Point3D;

    $self->init($x, $y, $z);

    return $self;
  }

  protected method init : Point3D ($x = 0 : int, $y = 0 : int, $z = 0 : int) {
    $self->SUPER::init($x, $y);
    $self->{z} = $z;
  }

  method clear : void () {
    $self->SUPER::clear;
    $self->{z} = 0;
  }

  method clone : Point3D () {
    my $self_clone = Point3D->new($self->x, $self->y, $self->z);

    return $self_clone;
  }

  method to_string : string () {
    my $x = $self->x;
    my $y = $self->y;
    my $z = $self->z;

    my $string = "($x,$y,$z)";

    return $string;
  }
}
Enter fullscreen mode Exit fullscreen mode

Symbolic Link Support

As we mentioned last week, this week we are working on a portable, symbolic link implementation that also works on Windows. You can see our progress here. To implement this, the Perl win32/win32.c source code would be greatly appreciated.

Once the symbolic links are working, we can achieve a great deal of portability between Windows/Linux/Mac. I look forward to implementing a browser that works on Windows/Mac/Linux/Android/iPhone/iPad with SPVM on top of this. My goal is to have a working game within 5 years.

Translated with www.DeepL.com/Translator (free version)

Top comments (0)