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

I want to read from a file, but Perl (internally) must not escape the string i read. E.g. of a line in file: 'c:\temp' This line is then handled as 'c:\\temp' - which I do not want. (Of course when using 'print $line' it automatically "unescapes" the line, but that is of no use to me. The second solution would be to read the file as is, and then "UnEscape" the text, but... how :) I'd appreciate the help.

Replies are listed 'Best First'.
Re: UnEscaping text...
by clemburg (Curate) on Aug 13, 2001 at 19:14 UTC

    You are confusing the value of the string with the literal representation of it. The string literal 'c:\\temp' has a value that is 7 chars long.

    > perl -e "print join ' ', split '', 'c:\\temp'" c : \ t e m p

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

      Hi! I am no longer anonymous :) The text file allready contains literal representation, so when i read from it i get a "literal representation of a literal representation", which is one lit. rep. too many :)
Re (tilly) 1: UnEscaping text...
by tilly (Archbishop) on Aug 13, 2001 at 19:29 UTC
    How are you reading such that you have any escaping or unescaping to do?

    If you have text which is part of Perl source-code, then you need to worry about \ escapes. Similarly if you have text on a command line, you need to worry about shell escapes. But if Perl is directly reading from a file, the only mangling that Perl should do is (depending on your platform) line endings that you can turn off with binmode. Just don't even try to unescape and you should be absolutely fine.

      I am reading from a text file, but the thing is: the text file allready contains escaped text - so when I read it, i get a "double escaped" text. I'd need a function, that "unescapes" a string, or "tell" Perl not to escape the strings, i read from a txt file. ('c:\\abc' becomes 'c:\\\\abc' internally. I need to unescape 'c:\\\\abc', so I can then print "c:\\abc" to get 'c:\abc' on file or STDOUT. And the string are not just like 'c:\\abc', but also include other special chars. Have a nice day!
        What you think is happening is not.

        Please give a short code example and give the actual contents of the file so we can track down what is really going on.

        Here is a simple case for you to consider. Create a file called "raw_text" that looks like this:

        hello world. This is a backslash: \ Here are 10 of them: \\\\\\\\\\
        Now create a small Perl script that looks like this:
        #! /usr/bin/perl -w use strict; my $file = "raw_text"; open(FILE, "< $file") or die "Cannot open '$file': $!"; my $contents = join '', <FILE>; print "HERE ARE THE CONTENTS OF $file:\n"; print $contents;
        On my machine I get an output of:
        HERE ARE THE CONTENTS OF raw_text: hello world. This is a backslash: \ Here are 10 of them: \\\\\\\\\\
        As you see, Perl does not alter the text. You may be altering it, but Perl is going to be only doing what you said.

        Now if your file starts off with something other than what you want, then you will need to figure out how to manipulate it into what you want. But nobody is going to be able to do that unless you have a more precise description of the change than just, "escaped". Escaped by what rules? Your talking about doubling backslashes tells me that it isn't using HTML escape codes. But there are a lot of other things that it could be and I am not going to guess.

Re: UnEscaping text...
by abstracts (Hermit) on Aug 14, 2001 at 12:36 UTC
    Hello

    Let's start from the beginning:

    Your file contains the string c:\temp. When perl reads that file, the string representation of that line would be c : \ t e m p. Perl does not add an additional \ to that line. You do not need to do any escaping/unescaping. And no, perl does not escape the line when it's read and unescapes it when it is printed. It keeps the line as is.

    Try this:

    open F, "<myfile" or die "Cannot open myfile: $!\n"; my $str = <F>; print join'|',split//,$str;
    Hope his helps,,,

    Aziz,,,