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

I' just started to play with PERL to see how I can autogenerate c-code.
I insert special comments in my c-code with tags to locate both source for autogeneration as well as the target location.
I find myself writing duplicates of some string to be able to both match these special comments and to print them into the c-code.
Example:
$matchbegintag = '\/\* <sometag> \*\/'; $printbegintag = "\/\* <sometag> \*\/";

This is error prone, since I always have to change in two places.
Is there an operator that can force interpolation on single quoted strings?
Something like:
$matchbegintag = '\/\* <sometag> \*\/'; $printbegintag = force_interpolation($matchbegintag);

Thanks in advance,
Jonas

Replies are listed 'Best First'.
Re: Force interpolation on singel quoted strings
by broquaint (Abbot) on Jun 07, 2002 at 12:39 UTC
    Is there an operator that can force interpolation on single quoted strings?
    Just use simple double quoting, or a plain old assignment will do in your case (although I can't see why you'd need it).

    Also you don't need to escape the forward slash or the astericks[1], as it they will be literally quoted within single quotes. The only thing that can needs to be escaped within a single quotes string is the string delimiter and a backslash (authoritatively covered here by Abigail-II). Check out the perlop manpage for more details about the quoting mechanisms available in perl.
    HTH

    _________
    broquaint

    [1] while this is technically correct it doesn't seem to be so in your case (see. Abigail-II's node regarding how to correctly escape these meta-characters)

      The * does need escaping. Not because it's in single quotes, no, the string is in single quotes because he wants the backslash there! The backslash is there to make the regex engine match an asteriks.

      The way to go is not to force interpolation, but to go the other way around. Do something like:

      my $printtag = "/* <sometag */"; my $matchtag = quotemeta $printtag;
      Or (probably more convenient), to just have $printtag and surround that with \Q and \E when putting it in the regex.

      Abigail