dotc has asked for the wisdom of the Perl Monks concerning the following question:

To provide context, I'm trying to convert .pro (chord pro) to .crd (chord) files. I'm having trouble doing something similar to the following:
$LEFT_DELIMITER = '{\['; $RIGHT_DELIMITER = '}\]'; $tempstr = 'This is a string containing [Am]brackets.'; $chord = 'Am'; $tempstr =~ s/[$LEFT_DELIMITER]$chord[$RIGHT_DELIMITER]//; print $tempstr;
Running this outputs, This is a string containing Am]brackets. instead of, This is a string containing brackets. I think I need s///; to interpolate $LEFT_DELIMITER and $RIGHT_DELIMITER, but not to attempt to interpolate $tempstr itself. Am I confused?

Replies are listed 'Best First'.
Re: s/$foo// selective interpolation?
by suaveant (Parson) on May 15, 2001 at 17:29 UTC
    $chord[$RIGHT_DELIMITER] is being accessed as an array, try ${chord}[$RIGHT_DELIMITER]
                    - Ant

      Good catch! Better still: s/[$LEFT_DELIMITER]\Q$chord\E[$RIGHT_DELIMITER]//g

      Update: After testing, I'd actually rewrite this as:

      $LEFT_DELIMITER = '{['; $RIGHT_DELIMITER = '}]'; $tempstr = 'This is a string containing [Am]brackets.'; $chord = 'Am'; $tempstr =~ s/[\Q$LEFT_DELIMITER\E]\Q$chord\E[\Q$RIGHT_DELIMITER\E]//; print $tempstr;
      (note the lack of \ in the delimiter strings).

              - tye (but my friends call me "Tye")
        With the stipulation that you don't want $chord to possibly contain regexp info, very true.
                        - Ant