title: Perl regular expression variables and matched pattern substitution
body: <p>Can anyone explain regular expression text substitutions when the regular expression is held in a variable?  I'm trying to process some text, Clearcase config specs actually, and substitute text as I go.  The rules for the substitution are held in an array of hashes that have the regular expression to match and the text to substitute.</p>
  
  <p>The input text looks somthing like this:</p>
  
  <pre><code>element  /my_elem/releases/...  VERSION_STRING.020 -nocheckout
  </code></pre>
  
  <p>Most of the substitutions are simply to remove lines that contain a specific text string, this works fine.  In some cases I want to substitute the text, but re-use the VERSION_STRING text.  I've tried using $1 in the substitution expression but it doesn't work.  $1 gets the version string in the match, but the replacement of $1 doesn't work in the substitution.</p>
  
  <p>In these cases the output should look something like this:</p>
  
  <pre><code>element  -directory  /my_elem/releases/... VERSION_STRING.020 -nocheckout
  element  /my_elem/releases/.../*.[ch]  VERSION_STRING.020 -nocheckout
  </code></pre>
  
  <p>ie. One line input became two output and the version string has been re-used.</p>
  
  <p>The code looks something like this.  First the regular expressions and substitutions:</p>
  
  <pre><code>my @Special_Regex = (   
                    { regex =&gt; "\\s*element\\s*\/my_elem_removed\\s*\/main\/\\d+\$",                  subs =&gt; "# Line removed" },
                    { regex =&gt; "\\s*element\\s*\/my_elem_changed\/releases\/\.\.\.\\s*\(\.\*\$\)", 
                      subs =&gt; "element  \-directory  \/my_elem\/releases\/\.\.\. \\1\nelement  \/my_elem\/releases\/\.\.\.\/\*\.\[ch\]  \\1" }
  
                  );
  </code></pre>
  
  <p>In the second regex the variable $1 is defined in the portion (.*\$) and this is working correctly.  The subs expression does not substitute it, however.</p>
  
  <pre><code> foreach my $line (&lt;INFILE&gt;)
          {
          chomp($line);
          my $test = $line;
          foreach my $hash (@Special_Regex)
          {
              my $regex = qr/$hash-&gt;{regex}/is;
              if($test =~ s/$regex/$hash-&gt;{subs}/)
                  {
                  print "$test\n";
                  print "$line\n";
                  print "$1\n";
                  }
           }
  }
  </code></pre>
  
  <p>What am I missing?  Thanks in advance.</p>