Skip to content

Commit

Permalink
[doc] Add /some/ documentation for .subst
Browse files Browse the repository at this point in the history
  • Loading branch information
s-h-r-i committed Dec 25, 2012
1 parent 31748bc commit 3b5dbf4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/Str.pod
Expand Up @@ -424,4 +424,33 @@ Remove the white-space charecters from the end of a string. See also L<trim>.
Remove the white-space charecters from the beginning of a string. See also L<trim>.
=head2 Substitution and replacement operations
=head2 subst
Does string substitution, these behave like s/// does in perl5.
my $some-string = "Some foo";
my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
$some-string.=subst(/foo/, "string); # in-place substitution. $some-string is now 'Some string'
The syntax usually goes like this:
STRING_TO_REPLACE.subst(/PATTERN/, REPLACEMENT);
In the case of subst, REPLACEMENT can also be a closure:
my $i = 41;
my $str = "The answer is secret.";
my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
Here are other examples of usage:
my $str = "Hey foo foo foo";
$str.subst(/foo/, "bar", :g); # global substitution - returns Hey bar bar bar
$str.subst(/foo/, "no subst", :x(0)); # targeted substitution. Number of times to substitute. Returns back unmodified.
$str.subst(/foo/, "bar", :x(1)); #replace just the first occurrence.
$str.subst(/foo/, "bar", :nth(3)); # replace nth match alone. Replaces the third foo. Returns Hey foo foo bar
=end pod

0 comments on commit 3b5dbf4

Please sign in to comment.