in reply to Backslash Interpolation

Interpolation doesn't happen after data is stored in the string, so the problem is likely in the data retrieval and/or transport stage. That is to say that no matter what crud you have in $string, high-ASCII, control codes, or otherwise, print doesn't really do anything fancy (binmode aside, which controls the application of CR-LF conversions in DOS/Windows).

Specifically:
my $var = "\\what will I get?."; print $var; \what will I get?.
That will return the "interpolated" version.
my $var = '\\\\what will I get?.'; print $var; \\what will I get?.
That will print whatever you give it. Of course, you can always take the other way around and get some truly safe string data:
my $var = quotemeta("\\what will I get?."); print $var; \\what\ will\ I\ get\?\.
I don't think CGI does anything fancy with backslashes, so are you sure the data gets in there in the right format? As Graham pointed out, using quotemeta on your input data may help out.

Replies are listed 'Best First'.
Re: Re: Backslash Interpolation
by Anonymous Monk on Jun 14, 2001 at 16:52 UTC
    Thanks for having a look.
    The data seems to be getting there correctly- I used Data::Dumper to have a peek at the CGI object and in there the backslashes are all intact
    The problem with using quotemeta is that it escapes everything as your example showed. Ideally I'd like to be able to present the data exactly as it was input into the form.
      Further to my earlier reply

      Could you not do a basic string substitution to account for these characters.

      my $x='\\what will I get?.' $x =~ s/\\/\\\\/ print $x
      will convert the string '\\what will I get?.'
      into '\\\\what will I get?.'
      which when printed becomes

      \\what will I get?.

      $x =~ s/\\/\\\\/g
      will account for multiple instances of \ within the string
        Ah this gives interesting results
        my $string = '\\this is a test on \ escapes . '; $string =~ s/\\/\\\\/g; print $string;
        I get the following output:
        \\this is a test on \\ escapes .
        for reference this is on perl version 5.006001

      Note that Data::Dumper produces Perl code so, if you give Data::Dumper a string with a single backslash in it, then it will display a string literal with the backslash escaped, that is, the output will contain two backslashes.

      So I still think the slash is being lost before CGI.pm gets involved.

              - tye (but my friends call me "Tye")
        Ah that makes sense now. The Dumper output was throwing me.
        I assume that the only reason \\ has to interpolate to \ because \' interpolates to ' .Otherwise my $var='\'; wouldn't compile.
        Is there actually a real way of assigning a string literal without any interpolation occuring at all?