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

i need to quote a "\" for a split , i allready looked at the "quotes in perl" page but it didnt help .

Replies are listed 'Best First'.
Re: quote a \
by grinder (Bishop) on Mar 10, 2008 at 11:01 UTC

    You can also use the quotemeta function to escape magic punctuation characters.

    my $safe = quotemeta($str);

    If $str contained a backslash, it would be escaped correctly.

    • another intruder with the mooring in the heart of the Perl

Re: quote a \
by moritz (Cardinal) on Mar 10, 2008 at 10:16 UTC
    You can quote it, very surprisingly, with another backslash \.
    my @items = split m/\\/, $string;
Re: quote a \
by drip (Beadle) on Mar 10, 2008 at 10:18 UTC
    another \ would escape the "\" character..=)
Re: quote a \
by ikegami (Patriarch) on Mar 10, 2008 at 16:56 UTC
    The first argument of split is a regexp:
    split /\\/, ...

    If you use a string literal, you need to take into account that the resulting string will be treated as regexp. That means you need to escape the slash to form the right string, then escape the slash again to form the right regexp.

    split "\\\\", ...

    In short, don't use a string as the first argument of split :)

Re: quote a \
by Anonymous Monk on Mar 10, 2008 at 11:31 UTC
    sorry for double post but it seems my IDE screwed up i just tried it per command line and it works . thanks guys .
Re: quote a \
by Anonymous Monk on Mar 10, 2008 at 11:27 UTC
    none of these seem to work with my code .
      Show us the code.

      It should work with double backslashes, maybe your problem is somewhere else?