Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Recap: The Future of Perl 5

by RonW (Parson)
on Aug 23, 2018 at 23:05 UTC ( #1220966=note: print w/replies, xml ) Need Help??


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
      Agreed, but it's not core Perl

      I think it would aid clarity to your arguments immensely if you could choose a different word for what you want. The Perl community has been using "core" to mean precisely modules which are bundled with the perl distro since last millennium (and I know you know this too). It's confusing if you start to use it to mean something else.

        In fairness, the built-in Perl functions are associated with the package CORE:: too, so his terminology makes sense in that respect.

      I agree, your proposed syntax is much nicer.

      I did see somethings surprising.

      In:

      class Point { has Num ($x, $y); method inverted () { return Point->new( x => $y, y => $x ); } }

      you have $x instead of $self->x (likewise for $y).

      I found this surprising, and had to pause to think for a second or 2. How common is it in other OO languages to not need "self" to reference instance variables in instance methods? Personally, I don't recall ever seeing it done without "self". (Been so long since I last used C++ that I had forgotten.)

      More surprising was:

      class Cache::LRU { use Hash::Ordered; our $num_caches = 0; # class data (unused +in this example) my $x = Hash::Ordered->new; # private instance da +ta has UInt $max_size = 20; # public instance dat +a method set ( Str $key, $value ) { if ( $x->exists($key) ) { $x->delete($key);

      where $x is also an instance variable.

      (Just some additional thoughts.)

        I found this surprising, and had to pause to think for a second or 2. How common is it in other OO languages to not need "self" to reference instance variables in instance methods? Personally, I don't recall ever seeing it done without "self". (Been so long since I last used C++ that I had forgotten.)

        Java does this too. You can disambiguate (if you have a local variable of the same name), with this. For example, this.x.

        IIRC is Ruby using sigils for @instance and @@class variables..

        Python has self, JS has this.

        But JS is setting this implicitly, i.e. it's not the first argument like self in Python and Perl.

        You could mimic the semantics of JS and Ruby with closure variables holding references...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        update
        added references

        update

        PHP seems to have an implicit $this.

Re^2: Recap: The Future of Perl 5
by Laurent_R (Canon) on Aug 24, 2018 at 07:10 UTC
    Hi RonW,

    I did not know about this Class::Struct module, this seems to be quite interesting. I'll have a look at it. Thank you for having pointed to it.

Re^2: Recap: The Future of Perl 5
by LanX (Archbishop) on Aug 03, 2019 at 12:50 UTC
    >   use Class::Struct mass => $, radius => $;  

    How is this legal Perl syntax? A short glimpse into Class::Struct seems to indicate that you rather meant

      use Class::Struct mass => '$', radius => '$';  

    Right?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      You're right. It was in a reply I wrote quickly and didn't get to test before I posted.

      (Also, thanks for the doc:// linking tip.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1220966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2019-10-18 19:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?