Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Recap: The Future of Perl 5 (backwards comaptibility, RFC)

by LanX (Saint)
on Aug 31, 2018 at 18:23 UTC ( [id://1221471]=note: print w/replies, xml ) Need Help??


in reply to Recap: The Future of Perl 5

We can easily speculate about a new "sexy" syntax which "Easy things should be easy, and hard things should be possible"°.

The crucial problem for the acceptance is backwards compatibility , both semantically and syntactically.

Most of us are stuck with an older version of Perl before the team decides to upgrade, so it would be very important for acceptance if the "sexy" syntax could already be run there.

NB: It doesn't need to be fast ... just complete. referring to a new version for speed could be a good incentive and marketing.

let me suggest something, would this syntax be acceptable?

{ use class Point => ISA-LIST; my ($x, $y); my $z :ro; our @points; # class var sub inverted :public { return Point->new( x => $$y, y => $$x ); } } #end of scope

please note that use class Point could be shortend to class Point with Keyword::Simple for Perl > 5.13

The whole mechanism would work with a safe source filter˛ which just injects some boilerplate OO package header at the _beginning_, and a callback which is triggered at the end of scope.*

The callback would inspect the STASH of the package and use PadWalker to identify instance variables.

The indentified subs (like "inverted") would be patched and wrapped with initialising code mapping the first argument to $self ( a closure var from the boilerplate).

Wrapper would look like something like

my $old_meth = \&Point::inverted; *Point::inverted = sub { $self = shift @_; $x = \ $self->{x}; $y = \ $self->{y}; goto &$old_meth; }

( I deliberate used references for instance vars, to have a visual clue)

This and the use of attributes for more "exotic" features would allow us to stay comaptible to

  • the old perl model, since $self still works
  • and shorten the syntax like our competitors.
  • we could stay compatible to older OO code from the most popular OO frameworks (M** whatever) by using this syntax as proxy frontend
  • people comming from other languages like Python would feel instantly at home
  • we can not only auto-generate ->new but also go by the Python convention to have a constructor sub called Point()
(DISCLAIMER: I didn't check yet the timing of attributes and triggering of the scope callback yet)

Of course the "real" implementation would use dedicated opcodes and ammend the parser, hence be mmore performand.

Comments?

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

°) didn't that use to be Perl's motto? (update: yep)

*) NB you don't need the block if there is only one class per file.

˛) safe because the source filter is just injecting code at the beginning and doesn't attempt to parse Perl.

Replies are listed 'Best First'.
Re^2: Recap: The Future of Perl 5 (backwards comaptibility, RFC)
by LanX (Saint) on Aug 31, 2018 at 19:00 UTC
    > but also go by the Python convention to have a constructor sub called Point()

    to elaborate further

    this:

    class Point: def __init__(self, x, y): self.x = x self.y = y def inverted(self): return Point(self.y,self.x)

    could be translated to

    class Point; sub INIT($X,$Y) { $$x = $X; $$y = $Y; # or $self->{y} = $Y; } sub inverted { return Point($$y,$$x) # or Point->new($$y,$$x) # or Point->new($self->{y},$self->{x}) # or Point($self->{y},$self->{x}) # ... TIMTOWTDI }

    People comming from Python would instantly feel at home and little by little discover more elaborated possibilities by using :attributes.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-18 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found