wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> $newpw is entered as "1q@W3e$R";
> chop($newpw = <STDIN>);
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
> $newpw becomes "1q@W3e" (the "$R" is chopped).
This would normally not happen. I don't know
what you did else, but maybe your "quoting interpolation"
will happen only in your "test example", see:
...
my $command = 'echo'; # change this to 'changepw'
my $oldpw = '1q@W3e$Q' . "\n"; # this is how input would come from
my $newpw = '1q@W3e$R' . "\n"; # outside (note the '' quote chars!)
chomp $oldpw; # don't use chop(), always use chomp()
chomp $newpw; # if possible (as Tad already said)
# use the qq{ .. } operator to evade quote masking errors
system qq{ $command -newpw "$newpw" -oldpw "$oldpw" };
...
If your program is larger, put a
use strict;
use warnings;
...
on top of it and Perl will tell you where
unwanted interpolation might happen.
Regards
M.