Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/27262 to your computer and use it in GitHub Desktop.
Save masak/27262 to your computer and use it in GitHub Desktop.
$ cat lingering-named-parameter.p6
use v6;
class A {
sub format_line($line is rw, :$link_maker) {
my $result = $line;
if defined $link_maker {
my $link_regex = / \[\[ (<-[\]]>+) \]\] /; # /
while $result ~~ $link_regex {
$result .= subst( $link_regex, { $link_maker($0) } );
}
}
return $result;
}
sub format_paragraph($paragraph, :$link_maker) {
return map { format_line($^line, :$link_maker) },
$paragraph.split("\n");
}
method format($text, :$link_maker) {
return format_paragraph($text, :$link_maker);
}
}
say A.new.format("a [[link]], sir", { "TROGDOR!" });
say A.new.format("a [[link]], sir") # look, no closure
$ perl6 lingering-named-parameter.p6
a TROGDOR!, sir
a TROGDOR!, sir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment