Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm learning Perl 6 and I'm constantly blown away by amazing Perl 6 features, it's something I experienced only while I was learning a bit of Common Lisp.

I wish to know if in the Perl 6 MOP there is something similar to CLOS before/around/after methods.

Also, given that my experience with programming is mainly quick procedural scripts (and, sadly, one big system in classic asp and vb for work) , which books/resources can give me a good grasp of Perl 6 OOP philosophy? For CL I loved the CLOS book by Kleene, but Perl 6 approach seems different (Smalltalk-ish???).

Thank you very much for your wisdom.

  • Comment on Perl 6 OOP: before and after methods "CLOS style"

Replies are listed 'Best First'.
Re: Perl 6 OOP: before and after methods "CLOS style"
by Laurent_R (Canon) on Feb 19, 2017 at 09:36 UTC
Re: Perl 6 OOP: before and after methods "CLOS style"
by snoopy (Curate) on Feb 19, 2017 at 21:38 UTC
    'before' and 'after' modifiers can be done declaratively. But may need some meta-programming extensions.

    I've done a first cut at a basic solution. Module CLOS defines before and after method traits as below:

    use CLOS; class Bake { method preheat { say "preheating"; } method cook { say "cooking"; } method rest { say "resting"; } } class Cook is Bake { method preheat is before { say "...about to preheat"; } method rest is after { say "...resting done"; } } my $cook = Cook.new; $cook.preheat; say "----"; $cook.cook; say "----"; $cook.rest;
    Produces:
    perl6 -I . Bake.pm ...about to preheat preheating ---- cooking ---- resting ...resting done
    Contents of CLOS.pm follow: This could possibly be fleshed out into a Perl 6 module.

      Awesome!

      I didn't know the depth of metaprogramming in Perl 6, now I have pointers to many hours of study

Re: Perl 6 OOP: before and after methods "CLOS style"
by raiph (Deacon) on Feb 19, 2017 at 22:31 UTC
    Hi AM,

    Thanks for looking into Perl and visiting the monastery. :)

    something similar to CLOS before/around/after methods.

    The v6.c version of the Perl 6 language is supposed to have a similar feature called `wrap` such that, given this code:

    sub foo { say 42 } &foo.wrap: { say 'before'; callsame; say 'after' } foo;

    The `foo` call in the last line displays:

    before 42 after

    The above is equivalent to a CLOS `:around`.

    Be aware that, compared with Perl 5, Perl 6 is an immature upstart, and that's an understatement. I've tested that my v6.c compatible Rakudo (all Rakudos released in 2016 or later support v6.c) does the above correctly and I can see that a lot of simple cases are tested in S06-advanced/wrap.t but I also see lots of bugs in the bug queue with 'wrap' in their title, at least some of which are about this particular feature.

    A search of modules.perl6.org for 'before' yielded Method::Modifiers. I see recent commits by the author. The project's modules are tiny examples of MOP programming. They're probably worth a look over even if you don't actually `use Method::Modifiers;`.

    I see others have answered your other questions. Have The Appropriate Amount Of Fun....

      Excellent pointers, Method::Modifiers is very interesting.

      The authors hints to this Perl 5 module:

      Moose::Manual::MethodModifiers

      as his ispiration. So now I know that not only Perl 6 can have before/around/after methods but also Perl 5 has them! I never had the chance to use OOP in Perl 5 (I use it for all my personal scripts when they are not trivial or they have to be cross platform, no big programs so far) but now I'm going to study Moose.

        Fyi, there's some really great sounding new stuff in the pipeline from the guy who started Moose and who is now focused on p5-MOP. Here's a recent presentation on p5-MOP. The audio's poor and it's probably way too low level but Stevan's so manic it'll probably be fun to watch a few minutes of it...
Re: Perl 6 OOP: before and after methods "CLOS style"
by Your Mother (Archbishop) on Feb 19, 2017 at 16:44 UTC

    FWIW, StackOverflow’s best programming language answer category (answered:unanswered) is Perl6.

Re: Perl 6 OOP: before and after methods "CLOS style"
by reisinge (Hermit) on Feb 20, 2017 at 10:48 UTC

      I added the book to my safari just 3 days ago and just skimming the first chapters got me totally hooked on Perl 6

      I also tried to support brian d foy's Kickstarter campaign for Learning Perl 6 but donations seem blocked.

        I think the Kickstarter campaign is over and brian is already writing the book :-).

Re: Perl 6 OOP: before and after methods "CLOS style"
by adhrain (Sexton) on Feb 20, 2017 at 22:38 UTC

    Hi, I am the former Anonynous Monk and I just created my account in order to THANK YOU ALL.

    Your answers are very helpful and I can say that Perl and its community amaze me even more than before.

Re: Perl 6 OOP: before and after methods "CLOS style"
by raiph (Deacon) on Feb 22, 2017 at 04:55 UTC
    Perl 6 approach to OO seems different than CLOS's (Smalltalk-ish???).

    The Object Apocalypse covers some of the rationale for the Perl 6 OO system design. The Apocalypses were published around a decade ago but Perl 6 as it is today remains very close to the system Larry described then. That said, all of the Design docs were primarily addressed to folk who knew Perl 5 and wanted to help implement Perl 6, and the Object Apocalypse isn't a CLOS vs Perl 6 comparison, so...

    I last spent significant time playing with CLOS in the 90s. Reading through A Brief Guide to CLOS as a refresher and noting, in no particular order, possibly consequential apparent differences if I'm not mistaken (I may have made mistakes in the following which I'm writing in somewhat of a hurry):

    What CLOS calls "methods" are ordinary functions that support multiple dispatch. They float outside of CL classes. They don't get access to a `self`. Perl 6 has the same feature but uses the term "subs" (short for "subroutines") or more specifically "multi subs". The equivalent of a CLOS 'generic function' is a "proto sub".

    Perl 6 also has what it calls "methods" or more specifically "multi methods". These functions (or "routines" in Perl 6 parlance) are typically defined as part of classes and they do have access to `self`.

    Multiple dispatch in CLOS is always late bound (resolved at run time). Perl 6 supports compile time checking and resolution (if only zero or one leading candidate matches) as well as late bound multiple dispatch (if multiple leading candidates match or the leading candidate is unknown at compile time). See also my SO answer to a recent Perl 6 question.

    While Perl 6 provides good FP support, the language parser and compiler itself is built using OO features and the MOP and language braids (as yet not officially documented and clearly on the move right now) let userland code control compilation and the syntax and semantics of Perl 6 itself. Aiui CL leaves this to macros, which, while awesomely powerful in their own ways (see 007 for the Perl 6 project attempting to outdo that awesomeness), aren't nearly as user friendly and scalable/interoperable across the community of users.

    Perl 6 supports multiple dispatch against all types, not just objects, eg native ints and floats etc. out of the box. It sounds like that gets more complicated in CLOS. Also, individual values, like CLOS. Perl 6 adds arbitrary `where` clauses to the mix too. (And I'll throw in a mention here of autothreading too, which is akin to a parallel computation dispatcher thrown into the function dispatch mix.)

    Unlike CLOS, Perl 6 encapsulates attribute access.

    Like CLOS, Perl 6 supports arbitrary routine combination schemes as part of routine dispatch (though it doesn't build a before/around/after scheme into the standard language). In fact you can specify a dispatch scheme per object! (Yes, much of this stuff I'm piling up in this comment is major overkill for 99.9% of users' programs. Fortunately the language is carefully designed in such a way that 99.9% of users need never care about any of this because the language remains easy for easy stuff and fairly easy even for complex stuff.)

    Like CLOS, Perl 6 supports multiple inheritance but the primary OO composition tool is roles which are a generalization of what Smalltalk calls "traits".

    Like CLOS, Perl 6 supports changing the structure/nature of objects on the fly. See but and does.

    CLOS is "not a prototype language" according to this CLOS guide I'm reading whereas Perl 6 specifically supports both class-based and prototype-based OO.

    Like CLOS (well, like the MOP extension to CLOS), Perl 6 has a powerful MOP that makes it easy to create new object-systems by extending or changing the provided Perl 6 functionality and have these systems interoperate at the class/object level. Thus Perl 6 has foreign object system adapters that let you load classes and objects from other langs and sub-class those foreign classes and instantiate those foreign objects as if they were Perl 6 classes/objects (and, if the foreign language is sufficiently adaptable, the other way around too). Naturally the most mature of these adapters is the one for Perl 5 (example) but there's a growing list.

    Perl 6 takes its MOP down to the metal via 6model, unifying Perl 6 objects, foreign object systems, MoarVM's base services, and C structs.

    I'm glad I've now gotten to the end of the guide. ;)

    All of these goodies come with some obvious "short term" costs for ordinary users: poor performance and poor documentation. The Rakudo Perl 6 compiler has been speeding up 2x to 4x each year for the last 7 years but, compared to many other languages, including Perl 5, it can still be shockingly fat and slow for a lot of stuff. Similarly, the doc keeps on improving, and books have finally begun to arrive again this year (the last printed books were published way prematurely about 10 years ago), but expect limited or non-existent coverage of some of the features I listed above for a while yet, perhaps several years.

    But the vision remains a language that starts with a base, like Perl 5 did in 1994, and then evolves from there to who knows where, but this time with much cleaner and more cleanly extensible syntax and semantics and an architecture built to sustain 100 years of evolution.

      This detailed explanation (thank you again raiph) confirms the gut feeling I have about Perl 6: it's the first language that looks MORE powerful and expressive than CL; it has also his own baroque elegance. My knowledge of the fine intricacies of programming is too rough to have a clear rational understanding of this aspect, but after having tried many languages I feel that Perl 6 is "just right" in so many nuances it seems a programmer's dream come true.

      As far as speed and books coverage are concerned, well… I'm not concerned; optimization is on its way for what I can fathom and books are on the works. Maybe when Larry will release "Programming Perl 6" (the "butterfly book", or book series given the size of the language) I will be enough knowledgeable to understand fully all the nuances of such a big and deep language.

      In my personal pantheon of languages (C-Zeus, Perl 5-Ares, CL-Aphrodite, Clojure-Artemis, Smalltalk-Ermes) Perl 6 is going to take the place of Athena, that was vacant since I couldn't find a language that was beautiful, powerful, deep and wise at the same time.

      Could you give an example of how Perl6 supports prototype-based OO?