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
?
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.
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.
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.
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 Method
s are a subclass of Routine
s
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With