The Substitution Operator
#!/usr/bin/perl -w
######################### The Many Faces of s/// #####################
$var = "abc
def
ghi";
$var2 = $var;
$var2 =~ s/.$/X/; ######### "." matches the "i"!!
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/.$/X/g; ######### What if I put "g" at the end of s///?
print "$var2\n\n"; ######### The "." NEVER matches \n.
$var2 = $var;
$var2 =~ s/.$/X/m; ######### Substitute last character in "multi-line"
print "$var2\n\n"; ######### mode. Should change "c" to "X".
$var2 = $var;
$var2 =~ s/.$/X/mg; ######### Now "c", "f", and "i" should ALL become "X".
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/.$/X/s; ######### s///s is the default.
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/.$/X/sg; ######### In single line mode, $ exists in only 1 place!!
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/\n/X/; ######### Should change first newline to "X".
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/\n/X/g; ######### Change all newlines to "X" because of "g".
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/\n/X/gm; ######### No change in result because of explicit \n.
print "$var2\n\n";
$var2 = $var;
$var2 =~ s/\s/X/; ######### Change first whitespace to "X". Should change
print "$var2\n\n"; ######### First \n ONLY to "X".
$var2 = $var;
$var2 =~ s/\s/X/g; ######### Change all whitespace to "X". Should change
print "$var2\n\n"; ######### all \n to "X".
$var2 = $var;
$var2 =~ s/\s/X/gs; ######### Same output as last print. Single-line default.
print "$var2\n\n";
$var = "abcde:fg:hij:k:lmn";
$var =~ s/\b([a-z]+)\b/$1X/g; ###### \b is a word boundary.
print "$var\n";
######################### Results of Execution Below ########################
abc
def
ghX ######## "i" changed to X as predicted.
abc
def
ghX ######## No different from first line of output. Why? Because
######## s/// is in "single-line" mode unless you tell it
######## otherwise. Also, "." CANNOT match \n in ANY mode!!
abX ######## "c" changed to X as predicted.
def
ghi
abX ######## Yup, "c", "f", and "i" all changed to X.
deX
ghX
abc ######## Same outcome as first lines of output.
def
ghX
abc
def
ghX ######## Ditto!
abcXdef ######## Only first newline changed to "X" as predicted.
ghi
abcXdefXghi ######## All newlines changed to "X" because of s///g.
abcXdefXghi ######## No change in result due to explicit \n in s///mg.
abcXdef ######## First \n changed to "X" due to explicit \n in s///.
ghi
abcXdefXghi ######## All \n changed to "X" due to explicit \n in s///g.
abcXdefXghi ######## Same output because s///sg is same as s///g here.
abcdeX:fgX:hijX:kX:lmnX
######################## Advanced Substitutions #######################
#!/usr/bin/perl -w
$var = "abc123for456and789me!";
$changes = $var =~ s/\d//g;
print "$changes $var\n"; ## Output 1.
$var = "The file /etc/passwd is the password file.";
$var =~ s#/etc/passwd#/etc/FOOBAR#;
print "$var\n"; ## Output 2.
$var = "10 20 30 40 50";
$var =~ s/\d+/$& ** 2/ge; ###### $& means "pattern between the first
print "$var\n"; ###### set of //". Output 3.
$var = "10 20 30 40 50";
$var =~ s/\d+/$& x length($&)/ge; ## Output 4.
print "$var\n";
$var = "511 is an octal version of 511.";
$var =~ s/^\d+/sprintf("%o",$&)/e;
print "$var\n"; ## Output 5.
$var = " hEllo, I aM JohN!";
$var =~ s/.*/clean_line($&)/e;
print "$var\n"; ## Output 6.
$var = "123abcdefABC";
$howmany = $var =~ s/\d{3}|[A-Z]{3}//g;
print "$var $howmany\n"; ## Output 7.
$var = "1234abcd123abc12345ab23";
$var =~ s/\d{3}//g;
print "$var\n"; ## Output 8.
$var = "1234abcd123abc12345ab23";
$var =~ s/\d{3,}//g;
print "$var\n"; ## Output 9.
################### End of Main Routine #######################
sub clean_line ## First exposure to subs!!
{
my ($str) = @_;
$str =~ s/^\s*(.*?)\s*$/$1/;
$str =~ s/\s+/ /g; ### Multi-whitespace sequences ==> one ASCII space.
$str = lc($str);
return $str;
}
##################### Program Output Below ######################
9 abcforandme! ## Output 1.
The file /etc/FOOBAR is the password file. ## Output 2.
100 400 900 1600 2500 ## Output 3.
1010 2020 3030 4040 5050 ## Output 4.
777 is an octal version of 511. ## Output 5.
hello, i am john! ## Output 6.
abcdef 2 ## Output 7.
4abcdabc45ab23 ## Output 8.
abcdabcab23 ## Output 9.