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.)
In reply to Re^5: Thoughts on new 'class' OO in upcoming perl
by tobyink
in thread Thoughts on new 'class' OO in upcoming perl
by cavac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |