Search and Replace - Answer to exercise 1
$search_text = $ARGV[0];
$filename = $ARGV[1];
$capital_search_text = uc($search_text);
open FILE,"<$filename" or die "Cannot read the file $filename: $!\n";
while ($line = <FILE>)
{
if ($line =~ m/$search_text/)
{
$line =~ s/$search_text/**$capital_search_text**/ig;
print $line;
}
}
Comments
**$capital_search_text**
may i know what does this mean?
Admittedly confusing ;-)
**$capital_search_text** is just the value of $capital_search_text surrounded by two asterisks, e.g. if $capital_search_text was equal to "SOME_TEXT", then this would equal "**SOME_TEXT**".
The asterisks aren't significant - I just added them to make the text stand out.