in reply to Problem with interpolating backslash

Please do not use prototypes if you don't know why they are problematic. Search for prototypes for more info.

#!/usr/bin/perl -w use strict; sub fixn($) { my $str = "@_"; $str =~ s/[^a-zA-Z0-9^_\-\\`\.\[\]\{\}~]//g; return $str; } sub fixn2 { # no prototype needed my $str = shift; # since we only ever want 1 argument # which will be converted to a string # if needed anyway. $str =~ s/[^a-zA-Z0-9^_\-\\`\.\[\]\{\}~]//g; return $str; } my $input = 'a b \\ c d'; print "input='$input'\n"; my $myvar = fixn($input); print "myvar='$myvar'\n"; my $better = fixn2($input); print "better='$better'\n";
output:
input='a b \ c d' myvar='ab\cd' better='ab\cd'

In other words, your regex is fine, but your code is sloppy, and your problem is probably somewhere in do_whatever_I_want_with()

update:

I have the feeling, that backslashes are lost in my $str = "@_"; <= that quotes turn them into a escape sequences.

That doesn't happen: backslashes are only interpreted in literal strings, they are not "recursively" interpreted in interpolated variables. The only exception is when you embed a variable in a regex, like so:

$var = "bla[abc]\\t"; # $var contains bla[abc]\t /$var/; # is equivalent to /bla[abc]\t/ /\Q$var\E/; # is equivalent to /bla\[abc\]\\t/

Replies are listed 'Best First'.
Re^2: Problem with interpolating backslash
by pg (Canon) on Aug 20, 2005 at 16:06 UTC
    "Please do not use prototypes if you don't know why they are problematic."

    Also ;-), please do not use prototypes if you know why they are problematic. It is generally a bad idea to use Perl prototype.

      I solved the problem :)
      Instead  my $str = "@_"; I used  my $str = $_[0]; which is the same as shift.
      Interesting ideas, I'll meditate on them :)