| String-Gsub documentation | Contained in the String-Gsub distribution. |
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.
process global substitute, and return new string.
like gsub, but replace self string and return itself.
process one substitute, and return new string.
like subs, but replace self string and return itself.
| String-Gsub documentation | Contained in the String-Gsub distribution. |
## ---------------------------------------------------------------------------- # String::Gsub::Functions # ----------------------------------------------------------------------------- # Mastering programmed by YAMASHINA Hio # # Copyright 2006 YAMASHINA Hio # ----------------------------------------------------------------------------- # $Id$ # ----------------------------------------------------------------------------- package String::Gsub::Functions; use warnings; use strict; use String::Gsub::Matcher; use base qw(Exporter); our @EXPORT_OK = qw(gsub gsubx subs subsx); 1; # ----------------------------------------------------------------------------- # gsub($str, $regex, $replacement); # $regex is qr// or "string". # $replacement is sub{} or "string". # returns new value. # sub gsub($$$) { splice(@_, 0, 1, $_[0]); # copy. &gsubx; } # ----------------------------------------------------------------------------- # gsubx($str, $regex, $replacement); # $regex is qr// or "string". # $replacement is sub{} or "string". # returns new value and $str is replaced with result too. sub gsubx($$$) { #my $str = $_[0]; my $re = $_[1]; my $sub = $_[2]; ref($re) or $re = qr/\Q$re\E/; ref($sub) or $sub = do{ my$tmpl=$sub; sub{ shift->expand($tmpl); }; }; $_[0] =~ s{$re} { my $match = String::Gsub::Matcher->new($_[0]); $sub->($match); }ge; $_[0]; } # ----------------------------------------------------------------------------- # subs($str, $regex, $replacement); # $regex is qr// or "string". # $replacement is sub{} or "string". # returns new value. # sub subs($$$) { splice(@_, 0, 1, $_[0]); # copy. &subsx; } # ----------------------------------------------------------------------------- # subsx($str, $regex, $replacement); # $regex is qr// or "string". # $replacement is sub{} or "string". # returns new value and $str is replaced with result too. sub subsx($$$) { #my $str = $_[0]; my $re = $_[1]; my $sub = $_[2]; ref($re) or $re = qr/\Q$re\E/; ref($sub) or $sub = do{ my$tmpl=$sub; sub{ shift->expand($tmpl); }; }; $_[0] =~ s{$re} { my $match = String::Gsub::Matcher->new($_[0]); $sub->($match); }e; $_[0]; } __END__
# ----------------------------------------------------------------------------- # End of File. # -----------------------------------------------------------------------------