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

B

Bad Ynfo

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?

TIA
 
G

Gunnar Hjalmarsson

Bad said:
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?

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.
 
P

Paul Lalli

Bad Ynfo said:
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
 
U

Uri Guttman

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
 
E

Eugene Mikheyev

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/
 
U

Uri Guttman

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
 
T

Tad McClellan

Bad Ynfo said:
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#/#\\#;
 
B

Bad Ynfo

Paul Lalli said:
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!
 
E

Eugene Mikheyev

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...
 
B

Bad Ynfo

Tad McClellan said:
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
the (bad) mecanism =)
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).




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.

Thanks for your time and for your detailed answer.

B.Y.
 
T

Tad McClellan

Bernie Cosell said:
} 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.
 
1

187

Bernie said:
} 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? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top