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

Discussion in 'Perl Misc' started by Bad Ynfo, Oct 6, 2004.

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

>
> '', 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?

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

3. Paul LalliGuest

"Bad Ynfo" <> wrote in message
news:...
> 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?

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 Paul Lalli, Oct 6, 2004 4. Uri GuttmanGuest >>>>> "PL" == Paul Lalli <> writes: >> 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 ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Uri Guttman, Oct 6, 2004
5. Eugene MikheyevGuest

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

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

Eugene Mikheyev, Oct 6, 2004
6. Uri GuttmanGuest

>>>>> "EM" == Eugene Mikheyev <> writes:

>> 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{\\foo\bar} ; print $x' \foo\bar he wants 2 \'s at the beginning of the string, hence his 4 \'s. uri -- Uri Guttman ------ -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org Uri Guttman, Oct 6, 2004 7. Tad McClellanGuest Bad Ynfo <> wrote: > Hi, > > In perlop, it reads: > > '', 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'? Why do you think you want to assign '\\foo\bar' to a scalar without having to say '\\\\foo\bar'? That is NOT a rhetorical question. This sounds like an XY Problem to me, we could give a much better answer if we knew what the "Y" was... What is it that you are ultimately trying to accomplish? If those data are paths, then did you know that '//foo/bar' will work in most cases? How are you using these strings? If you can switch to sensible slashes, your whole problem goes away (which is what makes those silly slashes silly). > Is it possible? Sure, but the cure is MUCH worse than the disease. What is your objection to backslashing backslashes in literal strings? Here are some examples:$_ = chr(92) . chr(92) . 'foo' . chr(92) . 'bar'; # works for ASCII

$_ = "@{[chr(92)]}@{[chr(92)]}foo@{[chr(92)]}bar"; # works for ASCII my$bs = chr(92); # works for ASCII
$_ = "$bs${bs}foo${bs}bar";

$_ = '//foo/bar';$_ =~ tr#/#\\#;

--
Perl programming
Fort Worth, Texas

"Paul Lalli" <> wrote in message news:<vwT8d.695$tc.258@trndny02>... > "Bad Ynfo" <> wrote in message > news:... > > Hi, > > > > In perlop, it reads: > > > > '', 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? > > 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

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

9. Eugene MikheyevGuest

> have you tried that out and printed it?
>
> perl -le '$x = q{\\foo\bar} ; print$x'
> \foo\bar
>
> he wants 2 \'s at the beginning of the string, hence his 4 \'s.
>
> uri

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

Eugene Mikheyev, Oct 7, 2004

Tad McClellan <> wrote in message news:<>...
> > Hi,
> >
> > In perlop, it reads:
> >
> > '', 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'?

>
>
> Why do you think you want to assign '\\foo\bar' to a scalar without
> having to say '\\\\foo\bar'?

Because the assignment is made from a text file where it reads
\\foo\bar!
The "transfer" from the file is made by means of "copy-and-paste".
Sure it's not a good method since we can't process the input file, but
I can't change this step and I wanted a quick solution to handle this
situation.

The single-quoted heredoc should do the trick without having to change

>
> That is NOT a rhetorical question. This sounds like an XY Problem to me,
> we could give a much better answer if we knew what the "Y" was...
>
>
> What is it that you are ultimately trying to accomplish?
>
>
> If those data are paths, then did you know that '//foo/bar' will
> work in most cases?

I know this possibility but because of the "copy-and-paste", this was
not a solution...

>
> How are you using these strings?
>
> If you can switch to sensible slashes, your whole problem goes away
> (which is what makes those silly slashes silly).
>
>
> > Is it possible?

>
>
> Sure, but the cure is MUCH worse than the disease.
>
> What is your objection to backslashing backslashes in literal strings?

copy-and-paste =)

>
> Here are some examples:
>
> $_ = chr(92) . chr(92) . 'foo' . chr(92) . 'bar'; # works for ASCII > >$_ = "@{[chr(92)]}@{[chr(92)]}foo@{[chr(92)]}bar"; # works for ASCII
>
> my $bs = chr(92); # works for ASCII >$_ = "$bs${bs}foo${bs}bar"; > >$_ = '//foo/bar';
> \$_ =~ tr#/#\\#;

Unfortunately these examples are not relevant to my problem.

B.Y.

Bernie Cosell <> wrote:
> Uri Guttman <> wrote:
>
>} >>>>> "PL" == Paul Lalli <> writes:
>
> } 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 \.

> Why does that apply if you use 'q' with an explicit terminator?

For the same reason as when using single quotes, to escape the terminator.

> Is there a need to scan for '\' if
> I'm doing: q{stuff} ?

Yes, as you might need a

\}

in there somewhere.

--
Perl programming
Fort Worth, Texas

12. 187Guest

Bernie Cosell wrote:
> Uri Guttman <> wrote:
>
> } >>>>> "PL" == Paul Lalli <> writes:
>
> } 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{stuff} ?

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

187, Oct 7, 2004
13. 187Guest

Abigail wrote:
> Tad McClellan () wrote on MMMMLV September
> MCMXCIII in <URL:news:>:
>>> Bernie Cosell <> wrote:
>>>> Uri Guttman <> wrote:
>>>>
>>>> } >>>>> "PL" == Paul Lalli <> writes:
>>>>
>>>> } 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 \.
>>>
>>>
>>>> Why does that apply if you use 'q' with an explicit terminator?
>>>
>>>
>>> For the same reason as when using single quotes, to escape the
>>> terminator.
>>>
>>>
>>>> Is there a need to scan for '\' if
>>>> I'm doing: q{stuff} ?
>>>
>>>
>>> Yes, as you might need a
>>>
>>> \}
>>>
>>> in there somewhere.

>
>
> Or a \{

Or a \\

187, Oct 8, 2004