Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl 5.36.0 synopsis example doesn't work for feature 'class'

Tags:

perl

I'm taking an example from Perl's documentation:

https://perldoc.perl.org/blead/perlclass

and trying to execute the following script:

use v5.36;
use feature 'class';

class My::Example 1.234 {
    field $x;

    ADJUST {
        $x = "Hello, world";
    }

    method print_message {
        say $x;
    }
}

My::Example->new->print_message;

but I get this error:

Feature "class" is not supported by Perl 5.36.0 at /home/con/Scripts/class.pl line 4.
BEGIN failed--compilation aborted at /home/con/Scripts/class.pl line 4.
Command exited with non-zero status 255

I'm copying the script exactly from the manual, yet the script doesn't work.

Is this a bug, or am I doing something wrong?

like image 905
con Avatar asked Oct 16 '25 09:10

con


1 Answers

Despite the use v5.36; in that example, the class keyword was not introduced until Perl v5.37.9. That is an unstable Perl version though.

It is expected (and by "expected", I mean "almost a foregone conclusion") that Perl v5.38 will be the first stable version of Perl to include support for the class keyword. That will likely be released in the next few weeks. The class keyword will be experimental though, so if you want to use it without warnings, you'll need to include this at the top of your file:

use v5.38;
use experimental 'class';

In the mean time, you should investigate Object::Pad which was the prototype implementation of this feature, and will run on older versions of Perl.

There's also a module called Feature::Compat::Class which will load either Perl's native class support or Object::Pad, depending on which is available.

like image 84
tobyink Avatar answered Oct 18 '25 06:10

tobyink