Skip to content

Instantly share code, notes, and snippets.

@moritz
Created February 26, 2011 20:58
Show Gist options
  • Save moritz/845617 to your computer and use it in GitHub Desktop.
Save moritz/845617 to your computer and use it in GitHub Desktop.
Named arguments for sprintf
# inspired by python's infix %
# see for example http://www.korokithakis.net/tutorials/python
sub named-sprintf(Str $format, *%args) {
my @args;
my $f = $format.subst(:g,
rx{ '%(' (\w+) ')' },
-> $/ { @args.push: %args{$0}; '%' }
);
sprintf $f, @args;
}
say named-sprintf 'This %(verb)s a %(noun)s', :verb<is>, :noun<test>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment