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 prop…


This content originally appeared on DEV Community and was authored by Yuki Kimoto - SPVM - Perl Club

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;
  }
}

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)


This content originally appeared on DEV Community and was authored by Yuki Kimoto - SPVM - Perl Club


Print Share Comment Cite Upload Translate Updates
APA

Yuki Kimoto - SPVM - Perl Club | Sciencx (2023-03-10T22:27:15+00:00) SPVM now supports object-oriented programming in Perl. Retrieved from https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/

MLA
" » SPVM now supports object-oriented programming in Perl." Yuki Kimoto - SPVM - Perl Club | Sciencx - Friday March 10, 2023, https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/
HARVARD
Yuki Kimoto - SPVM - Perl Club | Sciencx Friday March 10, 2023 » SPVM now supports object-oriented programming in Perl., viewed ,<https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/>
VANCOUVER
Yuki Kimoto - SPVM - Perl Club | Sciencx - » SPVM now supports object-oriented programming in Perl. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/
CHICAGO
" » SPVM now supports object-oriented programming in Perl." Yuki Kimoto - SPVM - Perl Club | Sciencx - Accessed . https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/
IEEE
" » SPVM now supports object-oriented programming in Perl." Yuki Kimoto - SPVM - Perl Club | Sciencx [Online]. Available: https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/. [Accessed: ]
rf:citation
» SPVM now supports object-oriented programming in Perl | Yuki Kimoto - SPVM - Perl Club | Sciencx | https://www.scien.cx/2023/03/10/spvm-now-supports-object-oriented-programming-in-perl/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.