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

I'm trying to get a string from a regexp, then pass it to a function I made, to remove special symbols from it and save it (to a file, for example). If that inital string comes with a "\" somewhere whitin, after I pass it trought the function, it disappears. I don't want that. Here is how I do it:
if ($line =~ /blablabla :(.+?): bleblable/){ $my_var = fixn($1); do_whatever_I_want_with($my_var); }
Here is the function:
sub fixn($){ my $str = "@_"; $str =~ s/[^a-zA-Z0-9^_\-\\`\.\[\]\{\}~]//g; chomp $str; return $str; }
I have the feeling, that backslashes are lost in my $str = "@_"; <= that quotes turn them into a escape sequences.
Any idea how to keep the backslashes?

Replies are listed 'Best First'.
Re: Problem with interpolating backslash
by Joost (Canon) on Aug 20, 2005 at 12:22 UTC
    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/
      "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 :)
Re: Problem with interpolating backslash
by davido (Cardinal) on Aug 20, 2005 at 15:56 UTC