The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


String::Gsub::Functions - core functions of String::Gsub


 use String::Gsub::Functions qw(gsub);
 print gsub("abcabc", qr/(b)/,sub{uc$1}); # ==> "aBcaBc"
 gsubx(my $str = "abcabc", qr/(b)/, sub{uc $1});
 print $str; # ==> "aBcaBc";


This module has folloing functions:

 gsub ($str, $regexp, $replacement)
 gsubx($str, $regexp, $replacement)
 subs ($str, $regexp, $replacement)
 subsx($str, $regexp, $replacement)

$regexp is regular expression (qr//). And $replacement is code reference, which is invoked at replacement. In $replacement subroutine, match variables ($1, $2, ...) are avaiable like replacement part of s/PATTERN/REPLACEMENT/.

Both $regexp and $replacement can be string, but there are difference from usual part.

String passed on $regexp is not treated as regular expression , just string. special chars will be escaped.

String passwd on $replacement will be replaced some substrings with match strings and used as replacement string. \&, \` and \' are also did.


This module can export gsub, gsubx, subs, subsx.


gsub($str, $regexp, $replacement)

process global substitute, and return new string.

gsubx($str, $regexp, $replacement)

like gsub, but replace self string and return itself.

subs($str, $regexp, $replacement)

process one substitute, and return new string.

subsx($str, $regexp, $replacement)

like subs, but replace self string and return itself.