in reply to Substitute \W but not \.

The tr// (“transliteration”) operator might come in useful here. Particularly with the “c” modifier which complements (inverts) the search-list. You can use this modifier in conjunction with a list of all the characters you will accept, to transform all occurrences of characters not in this list into an empty-string.

See: perldoc perlop, which includes some fairly specific examples.

There are also specific File packages in CPAN which handle such things as “clean up this filename in an appropriate way for this system whatever-it-is,” which might be what you really want to achieve. It bears remembering, always at the front of your mind, that no matter what you are trying to accomplish (... to accomplish ... not to do ...), somebody on CPAN has probably already been there. If that's the case, you can probably just “hitch a ride on their broom.”