The first two parts are fine. You grab your search an replacement patterns and chomp of the newlines (you don't need the parens with chomp BTW).
Next it gets a bit confused. The <> construct is essentially the same as <STDIN> (see ChemBoy's post below). Within the while loop each input from STDIN is aliased to the magical $_ perl special variable. Here is some working code:
print "\nSearch Pattern: "; chomp(my $pattern = <>); $pattern = uc quotemeta $pattern; print "\nReplacement Pattern: "; chomp(my $replacement = <>); print "\nInput: "; while (<>) { chomp; last unless $_; s/$pattern/$replacement/ig; print; print "\nInput: "; }
The input sections are almost the same but I demonstrate a good use for parens with chomp to do in one line what was two before. I have also declared $pattern and $replacement as lexically scoped variables with my - this is a good idea otherwise all your variables will be globals. The $pattern = uc quotemeta $pattern does two things. The uc() function converts $pattern to UPPER CASE as desired and the quotemeta() escapes (with a \) all the meta charcters that can break your regex because the take on a non literal meaning in a regex.
We then demand some input from STDIN in an infinite while loop. As I noted this is assigned to the magical $_ var. This var is the default target of many perl functions. You see that I do a chomp, substitution and print without specifying what to do them on! They all target $_ - you either love this behaviour or hate it. I have added a last unless $_ so if we enter a null string we break out of this infinite loop. In perl 0, undefined and the null string '' are all false - everything else is true. With the substitution regex the /ig at the end are modifiers. the /i means ignore case (which we need to as we uppercased our $pattern) and the /g stands for global as in replace all occurences.
Hope this helps. Have fun and welcome.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: Searching and Replacing
by tachyon
in thread Searching and Replacing
by Satira
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |