in reply to Re^4: Thoughts on new 'class' OO in upcoming perl
in thread Thoughts on new 'class' OO in upcoming perl

A slightly neater way:

use 5.37.9; no warnings qw( experimental ); use feature qw( class defer ); use builtins qw( true false ); class No::New { use Carp; my $allow_construction = false; ADJUST { croak "Directly calling @{[ __PACKAGE__ ]}->new is forbidden" unless $allow_construction; } sub create { my $class = shift; $allow_construction = true; defer { $allow_construction = false }; return $class->new; } method text { "Congratulations!"; } } say No::New->create->text;

(Note: I haven't actually tried this as the most recent Perl I have installed is 5.37.2.)

Replies are listed 'Best First'.
Re^6: Thoughts on new 'class' OO in upcoming perl
by haj (Vicar) on Mar 06, 2023 at 21:38 UTC

    A nice demonstration of the new defer feature!

    My less neat way is required only if you want to prohibit another call to No::New->new that happens somewhere in the innards of an ADJUST block.