2
my $cmd = 'this is a "testin$g" ok';
print Dumper($cmd);

$VAR1 = 'this is a "testin$g" ok';

my $cmd = 'this is a "testin$g" ok';
$cmd =~ s/\$/\\\$/;
print Dumper($cmd);

$VAR1 = 'this is a "testin\\$g" ok';

I am trying to end up with a string that looks like this: 'this is a "testin\$g" ok'. A single \ in front of the $. But even though I replace the $ with \$ it ends up with two instead.

2 Answers 2

6

Your regex and string are both correct. Data::Dumper escapes the characters itself.

For printing a simple string just use

print $cmd."\n";

And you'll see it's ok.

Also, try this to see how Dumper escapes the characters

print Dumper('this is a "testin\$g" ok');
Sign up to request clarification or add additional context in comments.

Comments

1

Your substitution is correct. If you executed the following, you'd see that $cmd contains but a single backslash.

print("$cmd\n");    # this is a "testin\$g" ok

Data::Dumper prints a string literal that, when executed, would create the string in the structure.

$VAR1 = 'this is a "testin\\$g" ok';

indicates the variable dumped contains

this is a "testin\$g" ok

because

this is a "testin\$g" ok

gets assigned to $VAR1 when you execute

$VAR1 = 'this is a "testin\\$g" ok';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.