Description
Why are classes always strict?
My use case for using perl's new class feature is wrapping the AT protocol which backs the Bluesky social network. I started the project last year purely to work with perl's new OOP system. In AT.pm, I'm passing decoded JSON directly to class constructors (often recursively in the ADJUST
phase) so any public or private change they make to the backend ("lexicon" as they call them) potentially breaks my project. Production and live environment are one in the same for Bluesky so every commit they push with lexicon modifications could add new fields to a response. Often, the fields are part of features still under construction and essentially useless for clients but they break my project because perl classes choke on any unrecognized field.
I've spent a little time trying to figure out if and how I could mitigate the sky falling on my head randomly and quickly found the unfortunately named CONSTRUCT
phase as described in the wiki. I also read about some of the potential issues discussed in #77 but I didn't see anything directly related to the lede I'm eventually getting back to. perlclass
doesn't come with CONSTRUCT
support yet so I set aside time today to emulate it, manually writing dozens of functions to filter out parameters before passing what's left to the correct class constructor the same way I was manually writing getters before :reader
was included with 5.40.
It didn't take me more than a few minutes to realize that sifting through incoming parameters that way was... painfully tedious and, even worse, redundant. I'm already defining the list of expected parameters when I define the class so why would I do it a second time in my CONSTRUCT
phase? Then it hit me: why are classes always strict?
If classes are the future of OOP in Perl, does it not make sense that the strict and warnings pragmas become class aware like feature? Since use v5.12;
enables strict 'all'
and thus so does use v5.38
which is required to use classes, wouldn't it make sense to have
use v5.42; # I'm in the future, of course
no strict 'class';
disable, well, "strict" classes? Turned off, class.c
would warn instead of croak. To extend that, no warnings 'class';
would disable the warning as well. With both off, unrecognized (or 'unrecognised' as Paul is in the UK) parameters would be silently ignored.
Activity