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

Hi All,

I have problem with the regular expression while using the substitution operator.

$str1 = "accessagain"; $str2 = "@cce$$ag@!n"; $abc1 = "accessagain"; $abc2 = '@cce$$ag@!n'; $str1 =~ s/$str1/$str2/ ; $abc1 =~ s/$abc1/$abc2/ ; print " New String : $str1 : $abc1 \n";
Prints the output as
=> 18203@!n : @cce$$@g@!n
The problem is that when I have special characters in the variable and try to substitute the variable it is getting interpolated and thinking it as a perl variable.

I need to escape the special characters in the variable and it should be replaced. Actually my main requirement is that I am storing user passwords in a file for a small web-application in encrypted format.

When the user needs to update his password and use some special characters in it. The record is not getting updated since I am using the same above logic i.e the substitution operator to replace the existing record with the new record.

Is there any way to escape the special characters in the variable.

Please help me out in this regard. Thanks In Advance.

Sushil Kumar

Replies are listed 'Best First'.
Re: Escape the special characters while substitution
by ikegami (Patriarch) on Apr 24, 2009 at 18:35 UTC
    It has nothing to do with the substitution. The interpolation occurs before the assignment is even performed. You'd get a strict error has you been using use strict; as you should.
    $ perl -wle'use strict; my $str2 = "@cce$$ag@!n"; print $str2;' Global symbol "@cce" requires explicit package name at -e line 1. Global symbol "$ag" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $ perl -wle'use strict; my $str2 = "\@cce\$\$ag\@!n"; print $str2;' @cce$$ag@!n

    Now that the variables contain what you want them to contain, you still need to convert $str1 and $abc1 into a regexp pattern. quotemeta can be used to do that.

    my $str1_pat = quotemeta($str1); $str1 =~ s/$str1_pat/$str2/; - or - $str1 =~ s/\Q$str1\E/$str2/; # Same thing.

    But the fact that s/// is used at all is very fishy. s/// should be needed to change a password!

      The fact that the passwords are being stored at all, assuming that this is to authenticate a user, is fishy. If at all possible, do not store this type of information.

      If you (the OP, not ikegami) are just using this to authenticate someone coming into your system, create a "difficult" hash of the password, and store that. When the user tries to authenticate the next time, run the same hash function, and compare the results to your stored results.

      --MidLifeXis

      The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

      I never knew about the quotemeta function. This is awesome!
Re: Escape the special characters while substitution
by almut (Canon) on Apr 24, 2009 at 18:30 UTC
    Is there any way to escape the special characters in the variable

    quotemeta, or \Q within a double-quoted/interpolated string.