Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method/Sub Binding In Raku

I'd like to find out if there is a way to bind a method and/or a sub to another method/sub name in Raku. I've seen how you can bind a variable to a method/sub, but that's not quite what I'm looking for. I know how to do it in Perl 5:

sub sub1 {
  print "sub1!\n";
}

*sub2 = \&sub1;

sub1(); # sub1!
sub2(); # sub1!
like image 792
JustThisGuy Avatar asked Dec 06 '20 01:12

JustThisGuy


2 Answers

Actually, what you do with normal variables is pretty much exactly what you do with subs.

sub sub1 { say "sub1!" }

my &sub2 = &sub1;

sub1; # sub1!
sub2; # sub1!

You don't need to bind, actually, because subs are not containerized and &-sigiled variables don't have special handling for assignment like @ and %-sigiled variables. (If you do a .WHICH or .WHERE you can see that they point to the same place in memory).

like image 75
user0721090601 Avatar answered Nov 14 '22 15:11

user0721090601


@user0721090601 already gave the answer for subs. To do the same for methods is slightly more involved. Fortunately, there is a module in the ecosystem that makes that easier for you: Method::Also. This allows you to say:

use Method::Also;
# ...
method foo() is also<bar bazzy> {

And then you can call the .bar and .bazzy methods as well, and get the same result as calling the .foo method.

like image 41
Elizabeth Mattijsen Avatar answered Nov 14 '22 15:11

Elizabeth Mattijsen