Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement around in Raku

Tags:

oop

raku

In Perl, using Moo, you can implement around subs, which will wrap around other methods in a class.

around INSERT => sub {
    my $orig = shift;
    my $self = shift;

    print "Before the original sub\n";
    my $rv  = $orig->($self, @_);
    print "After the original sub\n";
};

How can this behaviour be implemented in Raku, preferably using a role?

like image 514
Tyil Avatar asked Nov 06 '19 14:11

Tyil


People also ask

How do I get crackle in raku?

To achieve a good crackle, which is the hallmark of a great Raku pot, once the firing is complete and you have turned off the fuel and removed the kiln lid, allow the Raku kiln to cool down a little before removing your pots from the kiln.

Does raku need to be bisque fired?

First you must bisque fire your pots as usual. Make sure you use a clay that is designed for Raku firing. It will be an open body with good thermal shock characteristics. Next you can apply slip, apply glaze, or just leave the pot bare.

Can you refire a raku piece?

It gets a little more complicated with reduction firings, including Raku. Since these firings need a lack of oxygen in order for the glazes to develop, you can't refire them in an oxidation firing (electric kiln) or all the reduction you did will be reversed.


1 Answers

Use wrap

sub bar () { return "baþ" };

my $wrapped = &bar.wrap( { " → " ~ callsame() ~ " ← " } );

say bar(); # OUTPUT:  «→ baþ ← »

Since methods are routines, you'll need a slightly more convoluted way to get a handle on the method itself, but other than that, the method is exactly the same, since Methods are a subclass of Routines

class Baz {
    method bar () { return "baþ" };
}

my &method_bar = Baz.^find_method("bar");
my $wrapped = &method_bar.wrap( { " → " ~ callsame() ~ " ← " } );

say Baz.bar(); # OUTPUT:  «→ baþ ← »

The $wrapped is a handle that can be used, later on, to unwrap it if needed.

Edit: to add the code to get a handle on the class method, taken from here, for instance.

like image 163
jjmerelo Avatar answered Sep 19 '22 19:09

jjmerelo