in reply to Recap: The Future of Perl 5
Note that Perl does ship with a (still) core module called Class::Struct, which offers a simple syntax for OO programming.
An example I wrote 2 years ago is somewhat similar to the Point class example you provided. I only wrote it as a rebuttal to a claim that Perl OO programming is very hard without installing something from CPAN
package Planet; use Class::Struct mass => '$', radius => '$'; use Math::Trig; sub density { my $self = shift; my $vol = 4/3*pi*($self->radius)**3); return $vol/($self->mass); } package main; use Planet; my $Jupiter = Planet->new(mass => 1.8E27, radius => 6.9E7); print "Jupiter has mass of " . $Jupiter->mass . " and "; print "radius of " . $Jupiter->radius . "\n"; print "It's density is " . $Jupiter->density . "\n";
Not as clean as your proposed syntax, but is something that can be used without needing anything from CPAN. And there are limitations, such as Class::Struct classes can only inherit from UNIVERSAL.
Edit: corrected syntax as pointed out by LanX. Also, changed mod:// links to doc://
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Recap: The Future of Perl 5
by Ovid (Cardinal) on Aug 23, 2018 at 23:46 UTC | |
by hippo (Archbishop) on Aug 24, 2018 at 08:23 UTC | |
by tobyink (Canon) on Aug 24, 2018 at 10:54 UTC | |
by RonW (Parson) on Aug 27, 2018 at 20:05 UTC | |
by Ovid (Cardinal) on Aug 29, 2018 at 16:57 UTC | |
by LanX (Saint) on Aug 29, 2018 at 17:23 UTC | |
by tobyink (Canon) on Aug 30, 2018 at 08:15 UTC | |
Re^2: Recap: The Future of Perl 5
by Laurent_R (Canon) on Aug 24, 2018 at 07:10 UTC | |
Re^2: Recap: The Future of Perl 5
by LanX (Saint) on Aug 03, 2019 at 12:50 UTC | |
by RonW (Parson) on Aug 04, 2019 at 13:04 UTC |