Posts in this category

Wed, 26 May 2010

This Week's Contribution to Perl 6 Week 4: Implement :samecase for .subst

Permanent link

For this week's contribution to Perl 6 we ask you to implement an option in the string substitution backend that is be used for s/// style replacements.

(Introduction to this series of challenges)


In Perl 6, the string substitution operation s/// has a method backend. Instead of s:g/foo/bar/ (globally substitute foo by bar) you can write:

say 'some foo with sugar'.subst(/foo/, 'bar', :g);

Currently Rakudo doesn't parse the adverbs at the front, but supports the named arguments in the method form.

What you can do

Implement the :samecase named parameter. With this option, the case (upper/lower) of the original string is applied to the substitution:

say 'The foo and the bar'.subst(/:i the/, 'that', :g, :samecase);
# should produces 'That foo and that bar'

The actual case transformation is implemented in form of the samecase($substitution, $pattern) function already. If the replacement part is actually a code object, the samecase translation should apply to its return value.

Your task is to hook that function into the subst method in src/core/, and put some tests into t/spec/S05-substitutions/subst.t in the pugs repository that demonstrate that your code works.

When you've modified the test and/or the source file, you can run it with

make t/spec/S05-substitution/subst.t


Please submit your patches to the perl6-compiler mailing list, and CC' me ( As always, feel free to ask any questions about this question either on that mailing list, or on our IRC channel.

Update: A working patch from Hongwen Qiu has been applied, as well as some tests; more tests are still welcome.

[/perl-6] Permanent link