Passing regexes as arguments in Perl 6

别来无恙 提交于 2019-12-05 12:05:38

What you wrote is basically right, but you need to tweak the syntax a little. First, you should declare your combining function like any other sub. Next, it seems like to interpolate a regex into another, <$r> is the right syntax, and to interpolate a function call into a regex, <{my-sub(args)}> is the right syntax. (No need to prefix the sub with an ampersand when calling it—& is mostly for when you want to refer to a Callable without calling it.) Combine these little fixes and your code works:

sub combine(Regex $a, Regex $b --> Regex) {
    / <$a> <$b> /
}

my regex consonant { <[a .. z] -[aeiou]>  }
my regex vowel { <[aeiou]> }

my regex open_syllable { <{combine(&consonant, &vowel)}> }

"bac" ~~ m:g/ <open_syllable> /;
say ~$/; # output: ba
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!