Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Recap: The Future of Perl 5 (backwards comaptibility, RFC) by LanX
in thread Recap: The Future of Perl 5 by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found