4 ms·
The main highlight of this release is the new OOP system: https://perldoc.perl.org/5.38.0/perlclass https://perldoc.perl.org/5.38.0/perlclass
by xeeeeeeeeeeenu 3y ago
The main highlight of this release is the new OOP system: https://perldoc.perl.org/5.38.0/perlclass https://perldoc.perl.org/5.38.0/perlclass
- seanhunter 3y agoThat certainly seems way better than the old way of having to include the inscrutable line noise to construct, fill and then bless a hash reference in every single class, which is what we had to do back when I programmed perl.
- mfontani 3y agoSince about 2008-01 or so, using Moose, we've been able to do something like: { package Point; use Moose; has 'x' => (is => 'ro', isa => 'Int', required => 1); has 'y' => (is => 'ro', isa => 'Int', required => 1); sub as_string { my $self = shift; return sprintf '(%d,%d)', $self->x, $self->y; } } my $origin = Point->new(0, 0); say $origin->as_string;
- xorcist 3y agoMoose is one of those things I always mention when people ask what Perl did right. It wouldn't hurt other languages to have a higher order object templating system. Other things it did right is the taint flag to keep user supplied data from hitting sensitive functions by way of the type system. (In Rust you can declare your own type to achieve this, and it's actually enforced.) The unicode system was actually decent in ways nothing else is, at least until the Go and Rust generation. Python 3 wasn't nearly as complete despite being more than a decade later. If people ask what Perl did wrong, the dereferencing syntax is at the top of my list. Not having multidimensional arrays as a first class type probably did more to scare people away than anything else.
- matthewpersico 3y agoAfter the first deref, deref is assumed. Given: my $aref = [1,2,[3,4,5,[6,7] ] ]; You can do print $aref->[2][3][1]; which yields: 7 Or my @array = (1,2,[3,4,5,[6,7] ] ); print $array[2][3][1]; 7
- Ovid 3y agoAnd in the new class syntax, when it's done, that's (roughly): class Point { field $x :param :reader; field $y :param :reader; method as_string() { return "($x,$y)"; } } my $origin = Point->new( x => 0, y => 0 ); say $origin->as_string; I know which of the two I'd rather write :) The main limitation is that we don't have the `isa => 'Int'` check. That's largely because adding types is something for the entire language, not just this new syntax, so it had to be delayed.
- sixstringtheory 3y agoOh no, perl code is going to be hard to understand now
- doctor_eval 3y agoIt’s always been a write-only language.
- gpvos 3y agoWas it really not possible to make that a shorthand for the existing structure with blessing variables? Having two object systems may make things too confusing.
- leejo 3y agoThe prior "object system" was just a way to stuff things into the symbol table. I'm hand waving here, but the point is it that the scope of that system ended at "calling a method on an instance of a class" and there was nothing beyond it unless you rolled that all yourself: abstract classes derived classes / polymorphism attribute type constraints delegation encapsulation composition (roles) immutable state and so on And many people did roll their own, which resulted in Moose (Moo, et al) on CPAN, which have evolved to be some of the best in class (ha) object system frameworks and features across any language. Now the plan is to get that into the core, and this is the first step. No serious Perl developer has been using bless for OO for a least a decade, unless they were constrained by micro optimisations and/or not being allowed to use non-core dependencies.
- gpvos 3y agoI wasn't saying anyone seriously used that for OO. (There are still uses for tying.) But it would have been nice to have one integrated mechanism, with class syntax for high-level use and bless for low-level access. And maybe with some magic in the high-level syntax that you couldn't access with bless, like actually making members unreachable, that'd be fine. I assume it has been looked into and found to be impossible, but I'd like to know why.
- mst 3y agoRoughly, because (a) performance (b) the intention to makie it still possible to poke into an object's guts but more obvious that's what you're doing (c) if you really want the class syntax atop a classic blessed reference, p3rl.org/Object::Pad will do that if you configure the :repr (that's basically the cpan testbed for a lot of the ideas that have gone into core and written by the same LeoNerd)
- sigzero 3y agoThis is just the start of adding it in. They are adding the foundational things and then will be incrementally fleshing it out.