SPVM now supports object-oriented programming in Perl
Tags: perl, spvm
Published: 2023-03-10 22:27:15


<a href="https://github.com/yuki-kimoto/SPVM">SPVM</a> 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 <a href="https://github.com/yuki-kimoto/SPVM-File-Spec">SPVM::File::Spec</a> and <a href="https://github.com/yuki-kimoto/SPVM-IO/tree/master/lib/SPVM/IO">SPVM::IO</a>.

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, <a href="https://github.com/yuki-kimoto/SPVM/blob/master/lib/SPVM/Point3D.spvm">Point3D</a> and <a href="https://github.com/yuki-kimoto/SPVM/blob/master/lib/SPVM/Point.spvm">Point</a> 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;
  }
}
```

## Symbolic Link Support

<a href="https://dev.to/yukikimoto/first-release-of-spvmresourcere2-resourcing-the-regular-expression-library-google-re2-2j5a">As we mentioned last week</a>, this week <a href="https://github.com/yuki-kimoto/SPVM-Sys/tree/sys">we are working on a portable, symbolic link implementation</a> that also works on Windows. You can see our progress here. To implement this, the Perl <a href="https://github.com/Perl/perl5/blob/blead/win32/win32.c">win32/win32.c</a> 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)