ananthbv has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a program which takes in server name as a parameter, forms a connection string and calls a DBI->connect with that server name. The problem is if the server name has backslash inside it "\", it escapes it and as a result, the connection fails. Ex: $server = "SERVER05\LDEV" is converted to "SERVER05dev", the characters coming after the "\L" is changed to lower case.

I tried to find and replace \L to \\L or to space, but it does not seem to work. Is there a way to treat the string as 'raw', i.e., without escape chars? Kindly advise.

Replies are listed 'Best First'.
Re: replacing "\L" character in strings
by ww (Archbishop) on Jul 25, 2010 at 17:55 UTC
    • Put the path\filename inside single quotes (which do not interpolate)...
      or, possibly,
    • use path/filename which your script will (very high probability) use gracefully.
Re: replacing "\L" character in strings
by Marshall (Canon) on Jul 25, 2010 at 18:06 UTC
    Try "forward slash": /. Perl is amazingly smart about path names under Windows and without any exceptions that I can think of, / works better than \ for paths that Perl knows about.
      Actually, Perl is incredibly dumb about path names, passing the path unchanged to Windows. Windows accepts both "/" and "\" as path separators.

      Thanks ww and Marshall, sorry if I was not clear, but what is passed here is not path names but database server names and the server names have a backslash inside it. and if the letter coming after the backslash is a recognised escape character, it escapes it.

      Ex: if someone passes the server name as "SERVER05\LDEV", it is changed to "SERVER05dev" because \L is taken as 'convert all characters to lower case till a \E is found. Since the server name is different, the connection fails

      is there a way to ignore escape characters in strings or some other way?

        As you don't show any code, it's hard to tell you what you really mean. Maybe you want to use quotemeta respectively \Q in your regular expression, to prevent interpolation of regex-meta-characters? See perlre and/or perlop.

        The normal way to put a "backslash" in a character stream is to "back slash" the "backslash", i.e. with '\\', the first back slash means that the next char (which is a backslash) should be taken literally. In other words, convert single '\' to '\\'.
        Lacking any hint of how you've code the script, it's hard to be confident that my first suggestion -- that you use NON-INTERPOLATING SINGLE QUOTES (') -- will solve your problem... but it might, as might Corion's re quotemeta.
Re: replacing "\L" character in strings
by ikegami (Patriarch) on Jul 26, 2010 at 16:15 UTC

    "\" is special in string literals. You need to escape it.

    $server = "SERVER05\\LDEV"; # Produces SERVER05\LDEV $server = 'SERVER05\\LDEV'; # Produces SERVER05\LDEV $server = 'SERVER05\LDEV'; # Produces SERVER05\LDEV

    The third works since "\" only needs to be escaped when it's followed by another "\" or the string delimiter (') in single quoted strings.