in reply to Re: Backslash Interpolation
in thread Backslash Interpolation

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.

Replies are listed 'Best First'.
Re: Re: Re: Backslash Interpolation
by Graham (Deacon) on Jun 14, 2001 at 17:28 UTC
    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
        This is because your string has taken the single escape char and escaped it for you If you run perl in debug mode and look at the following it will show
        what I mean:
        $ perl -de0 Loading DB routines from perl5db.pl version 1.0402 Emacs support available. Enter h or `h h' for help. main::(-e:1): 0 DB<1> my $string = '\\this is a test on \ escapes . '; DB<2> print $string \this is a test on \ escapes . DB<3> x $string 0 '\\this is a test on \\ escapes . ' DB<4> $string =~ s/\\/\\\\/g; DB<5> print $string \\this is a test on \\ escapes .

        As you can see the evaluation of $string at step 3 shows it to now contain two backslashes where only one was entered.
        This is because it has evaluated correctly a single backslash escape but doesn't evaluate \\ correctly. I assume that it assumes you are escaping and it need do nothing.
        I think you're going to have to write a neat little subroutine accounting for all the above problems. A few smart substitution regex's combined with quotemeta should eventually cover all the bases....

(tye)Re: Backslash Interpolation
by tye (Sage) on Jun 14, 2001 at 20:27 UTC

    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?

        Yes, exactly. It is one of my pet peaves that q() makes \ special just for the sake of quoting a single character. In some ways I'd rather have ' be quoted by doubling it: 'This string isn''t ''valid'' in Perl'. But then things get really tricky if you try to extend that to deal with Perl's fancy delimiter schemes q(Too many ((s in string).

        The only quoting in Perl that doesn't treat \ as special is "here docs":

        my $string= <<"END"; Backslash (\) doesn't have to be doubled (\\) here. END
        That puts three backslashes into $string.

                - tye (but my friends call me "Tye")