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/

In reply to Re: Problem with interpolating backslash by Joost
in thread Problem with interpolating backslash by ivanatora

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.