in reply to How to replace '$' in strings

$string =~ s/\$/\\\$/g;

The replacement behaves like qq: \\ becomes \, and the dollar sing needs to be backslashed, too, to prevent the interpretation of $/.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: How to replace '$' in strings
by Discipulus (Canon) on Dec 14, 2015 at 10:22 UTC
    alternative regex delimiter can prevent also strange syndrome..
    perl -e "print $ARGV[0]=~s{\$}{\\\$}r" a$dollar a\$dollar or perl -e "print $ARGV[0]=~s{([\$\@\%])}{\\\1}gr" a$dollar@rray%%hashes a\$dollar\@rray\%\%hashes


    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Even simpler: use the special regex delimiter ' (single quote) to suppress interpolation:

      #! perl use strict; use warnings; my $string = 'i am us$ing moni$ka expressi$on'; $string =~ s'\$'\$'g; print $string, "\n";

      Output:

      23:12 >perl 1483_SoPW.pl i am us\$ing moni\$ka expressi\$on 23:14 >

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        special regex delimiter ' (single quote) to suppress Interpolation

        Thanks for the tip, nice to learn something new(*).

        For others: This is not in perlre, but under Regexp Quote Like Operators in perlop.

        (*) "new" to me, of course :-)