http://www.perlmonks.org?node_id=1078356

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

I am trying to write a subroutine which will do a global substitution on the global variable $_, then report some debugging information.
my ($nobrackets) = (qr/[^\{^\}]+/); &replace(qr/\{$nobrackets\}/,'$1') #This line is changed below. sub replace{ s/$_[1]/$_[2]/g }
The code does a nice job of finding the matches, but instead of removing the brackets, and keeping the text, it removes the brackets, and replaces them with the text with the literal '$1'.
my ($nobrackets) = (qr/[^\{^\}]+/); &replace(qr/\{$nobrackets\}/,"$1") #Using qq// instead of q//. sub replace{ s/$_[1]/$_[2]/g }
The first snippet of code will never replace the $1 with the captured text. The second snippet replaces the $1 before it captures the text. Is there a way to make perl interpolate the $1 after the call, and during the substitution?

=========================================================================== Update: 1:49 PM, March 15 (I have posted this clarification below, but I accidentally buried it in a subthread.)

Here is the code that I have working.

#!/usr/bin/perl $_='{\selectlanguage{english} \textcolor{black}{\ \ 10.\ \ Three resistors connected in series each carry currents labeled }\textit{\textcolor{black}{I}}\textcolor{black} +{\textsubscript{1}}\textcolor{black}{, }\textit{\textcolor{black}{I}}\textcolor{black}{\textsubscript{2}}\tex +tcolor{black}{and}\textit{\textcolor{black}{I}}\textcolor{black}{\tex +tsubscript{3}}\textcolor{black}{. Which of the following expresses the value of the total current }\textit{\textcolor{black}{I}}\textit{\textcolor{black}{\textsubscript +{T}}}\textcolor{black}{in the system made up of the three resistors i +n series?}}.';; $nobrackets = qr/[^\{}]+/; my $pass = 0; while(++$pass <=2){ s/\\textsuperscript\{($nobrackets)\}/ startsuperscript $1 endsuperscri +pt /g; s/\\textsubscript\{($nobrackets)\}/ startsubscript $1 endsubscript/g; s/\\textit\{($nobrackets)\}/ startitalic $1 enditalic/g; s/\\textcolor\{$nobrackets\}//g; s/\{($nobrackets)\}/($1)/g; print "Pass $pass:\n\n". qq{$_}."\n\n\n"; }
This produces output as follows:
Pass 1: {\selectlanguage(english) (\ \ 10.\ \ Three resistors connected in series each carry currents labeled )\textit{(I)}( startsubscript 1 endsubscript)(, )\textit{(I)}( startsubscript 2 endsubscript)(and)\textit{(I)}( starts +ubscript 3 endsubscript)(. Which of the following expresses the value of the total current )\textit{(I)}\textit{( startsubscript T endsubscript)}(in the system m +ade up of the three resistors in series?)}. Pass 2: (\selectlanguage(english) (\ \ 10.\ \ Three resistors connected in series each carry currents labeled ) startitalic (I) enditalic( startsubscript 1 e +ndsubscrip t)(, ) startitalic (I) enditalic( startsubscript 2 endsubscript)(and) start +italic (I) enditalic( startsubscript 3 endsubscript)(. Which of the following expresses the value of the total current ) startitalic (I) enditalic startitalic ( startsubscript T endsubscrip +t) endital ic(in the system made up of the three resistors in series?)).
Notice on pass 1, it removes the inner curly-brackets, and on pass 2, it removes the outer curly-brackets, additional passes could remove more curly-brackets if necessary. What I want(ed) to change was to turn these s///g or s///eeg statements into subroutines, keeping the capture and replacement variables separate. The code works fine as is, but I'm still curious as to whether the variables could be passed to a subroutine.