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

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

  1. Bad Ynfo

    Bad Ynfo Guest

    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
     
    Bad Ynfo, Oct 6, 2004
    #1
    1. Advertising

  2. Bad Ynfo wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 6, 2004
    #2
    1. Advertising

  3. Bad Ynfo

    Paul Lalli Guest

    "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
     
    Paul Lalli, Oct 6, 2004
    #3
  4. Bad Ynfo

    Uri Guttman Guest

    >>>>> "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
    #4
  5. > 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
    #5
  6. Bad Ynfo

    Uri Guttman Guest

    >>>>> "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
    #6
  7. 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#/#\\#;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 6, 2004
    #7
  8. Bad Ynfo

    Bad Ynfo Guest

    "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!
     
    Bad Ynfo, Oct 7, 2004
    #8
  9. > 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
    #9
  10. Bad Ynfo

    Bad Ynfo Guest

    Tad McClellan <> wrote in message news:<>...
    > 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'?


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

    Thanks for your time and for your detailed answer.

    B.Y.
     
    Bad Ynfo, Oct 7, 2004
    #10
  11. 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 7, 2004
    #11
  12. Bad Ynfo

    187 Guest

    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
    #12
  13. Bad Ynfo

    187 Guest

    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
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    580
  2. Guest

    Gridview Duplicate removal.

    Guest, Feb 7, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    6,837
    Yuan Ren[MSFT]
    Feb 15, 2006
  3. lucy
    Replies:
    6
    Views:
    1,306
    Carl Howells
    Sep 2, 2004
  4. Roedy Green

    break removal

    Roedy Green, Jul 2, 2005, in forum: Java
    Replies:
    9
    Views:
    436
  5. Octopus0
    Replies:
    2
    Views:
    654
    dorayme
    Mar 30, 2006
Loading...

Share This Page