I am writing a tiny program that takes user input using Getops, and based on it, the program will either try to match the template with some text, or replace the text for what matches.
The problem I am facing is that I cannot get the replacement part to work. I am looking at the qr // entry on the man pages: http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators , but I'm out of luck. In this case, I tried to simulate my code in the same way as the documents. I compile the match pattern and replace it with wildcard.
Can someone please indicate where I am going wrong? (Don't worry too much about security, this is just a little script for personal use)
Here is what I look at:
if($options{r}){ my $pattern = $options{r}; print "\nEnter Replacement text: "; my $rep_text = <STDIN>;
When I run it with -r "/ matt /" -i and type the replacement text 'matthew', it fails on the text 'matt'. Why is this?
EDIT:
Thanks for the responses guys! It was very helpful. I combined both of your suggestions into a working solution to the problem. I have to handle the / g flag a little differently. Here is a working example:
if($options{r}){ my $pattern = $options{r}; print "\nEnter Replacement text: "; my $rep_text = <STDIN>; chomp $rep_text;
regex perl substitution
radicalmatt
source share