title: Perl qr// and substitution
body: <p>I'm writing a tiny program that takes user input using Getops, and based on it, the program will either try to match a pattern against some text, or substitute text for what matched. </p>
  
  <p>The problem I'm having is that I can't get the substitution portion to work. I'm looking at the qr// entry in the man pages: <a href="http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators" rel="nofollow noreferrer">http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators</a> but I'm not having any luck with it. I tried to model my code exactly like the docs in this case. I compile a match pattern, and substitute that into a substitution. </p>
  
  <p>Could someone point out where I'm going wrong? (Don't worry about security too much, this is only a little script for personal use) </p>
  
  <p>Here's what I'm looking at:</p>
  
  <pre><code>if($options{r}){
  
      my $pattern = $options{r};
      print "\nEnter Replacement text: ";
      my $rep_text = &lt;STDIN&gt;;
  
      #variable grab, add flags to pattern if they exist.
      $pattern .= 'g' if $options{g};
      $pattern .= 'i' if $options{i};
      $pattern .= 's' if $options{s};
  
  
      #compile that stuff
      my $compd_pattern = qr"$pattern" or die $@;
      print $compd_pattern; #debugging
  
      print "Please enter the text you wish to run the pattern on: ";
      my $text = &lt;STDIN&gt;;
      chomp $text;    
  
      #do work and display
      if($text =~ s/$compd_pattern/$rep_text/){ #if the text matched or whatever
          print $text;
      }
      else{
          print "$compd_pattern on \n\t{$text} Failed. ";
      }
  } #end R FLAG
  </code></pre>
  
  <p>When I run it with -r "/matt/" -i, and enter the replacement text 'matthew', on the text 'matt', it fails. Why is this?</p>
  
  <p>EDIT: </p>
  
  <p>Thanks for the answers guys ! That was really 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 the working sample: </p>
  
  <pre><code>if($options{r}){
  
      my $pattern = $options{r};
      print "\nEnter Replacement text: ";
      my $rep_text = &lt;STDIN&gt;;
      chomp $rep_text;
  
      #variable grab, add flags to pattern if they exist.
  
      my $pattern_flags .= 'i' if $options{i};
      $pattern_flags .= 's' if $options{s};
  
      print "Please enter the text you wish to run the pattern on: ";
      my $text = &lt;STDIN&gt;;
      chomp $text;    
  
      #do work and display
      if($options{g}){
          if($text =~ s/(?$pattern_flags:$pattern)/$rep_text/g){ #if the text matched or whatever (with the g flag)
              print $text;
          }
          else{
              print "$pattern on \n\t{$text} Failed. ";
          }
      }
      else{
          if($text =~ s/(?$pattern_flags:$pattern)/$rep_text/){ #if the text matched or whatever
              print $text;
          }
          else{
              print "$pattern on \n\t{$text} Failed. ";
          }
      }
  } #end R FLAG
  </code></pre>