# How to prevent the removal of \ from pairs of \?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi,

'', q//
The only interpolation is removal of \ from pairs \.

So how can I assign '\foo\bar' to a scalar without having to say
'\\foo\bar'? Is it possible?

TIA

## Re: How to prevent the removal of \ from pairs of \?

About the only way I can think of is to use a single-quoted heredoc:

$path =<<'PATH'; \foo\bar PATH Note that I don't especially understand why this works. Why would single-quoted heredocs not work the same as 'normal' single-quoted strings? Paul Lalli ## Re: How to prevent the removal of \ from pairs of \? >> So how can I assign '\foo\bar' to a scalar without having to say >> '\\foo\bar'? Is it possible? PL> About the only way I can think of is to use a single-quoted heredoc: PL>$path =<<'PATH';
PL> \foo\bar
PL> PATH

PL> Note that I don't especially understand why this works.  Why would
PL> single-quoted heredocs not work the same as 'normal' single-quoted
PL> strings?

because in single quoted strings \ is needed to escape ' and \. in
single quoted heredocs there is no need to escape anything as perl just
scans for the closing token. in normal double quoted heredocs, perl has
to scan for \ escapes like \n and \t so it must handle \ and it makes
that into just \.

not that i have ever needed to use \ as data (i choose / path based
systems :), this is a good trick to know.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

## Re: How to prevent the removal of \ from pairs of \?

}   PL> Note that I don't especially understand why this works.  Why would
}   PL> single-quoted heredocs not work the same as 'normal' single-quoted
}   PL> strings?
}
} because in single quoted strings \ is needed to escape ' and \. in
} single quoted heredocs there is no need to escape anything as perl just
} scans for the closing token. in normal double quoted heredocs, perl has
} to scan for \ escapes like \n and \t so it must handle \ and it makes
} that into just \.

Why does that apply if you use 'q' with an explicit terminator?  [which
was, I thought, the original question.  Is there a need to scan for '\' if
I'm doing:  q ?

/Bernie\

--
Bernie Cosell                     Fantasy Farm Fibers
bernie@fantasyfarm.com            Pearisburg, VA
-->  Too many people, too few sheep  <--

## Re: How to prevent the removal of \ from pairs of \?

Bernie Cosell wrote:

It still needs to scan, how else do you think you would escape the
closing token? :-)

## Re: How to prevent the removal of \ from pairs of \?

Thanks Paul, that's exactly what I was looking for!

## Re: How to prevent the removal of \ from pairs of \?

I don't know of any other way.

For the case you are dealing with paths on Windows, you'd better use
straight slashes in Perl code.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

## Re: How to prevent the removal of \ from pairs of \?

my $re = '\foo\bar'; and then in the regex, use \Q..\E, i.e. /\Q$re\E/

## Re: How to prevent the removal of \ from pairs of \?

>> So how can I assign '\foo\bar' to a scalar without having to say
>> '\\foo\bar'? Is it possible?
EM> my $re = '\foo\bar'; have you tried that out and printed it? perl -le '$x = q ; print \$x'
\foo\bar

he wants 2 \'s at the beginning of the string, hence his 4 \'s.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

## Re: How to prevent the removal of \ from pairs of \?

Yes, you're right. I thought he was seeking for a regex without escaping
that slashes. So I did misfire...