I have a string,
my $element="abc#$def"
I escape # using,
$element=~s/#/\\#/g;
It is printed as: abc\#$def, which is perfect.
Next part of the code is:
push(@arr,$element);
foreach $val (@arr)
{
print $val;
}
And the value printed within the foreach loop is: abc#$def.
Why is # not escaped here? And how can I retain the escaping?
You’re not quite showing us everything. To get your claimed result, I had to create the variable
$definitialized as shown below. But, when I do that, I get the result you expect, not the result you show.This was tested with Perl 5.14.1 on MacOS X 10.6.8, but I don’t think the behaviour would vary with any other version of Perl 5.
Given this, can you update your question to show a script similar to mine (in particular, with both
use strict;anduse warnings;) but which produces the result you show?