$a =~ r/\W_//g;
There is no /g modifier for the tr/// operator. The transliteration operator also does not use regexp-like predefined character classes, such as \W. You probably meant something like:
$string =~ tr/a-zA-Z0-9.//cd;
This says to take the complement of the list created by the ranges a-z, A-Z, and 0-9, and delete anything not found among that range. Note, I didn't specifically deal with the underscore character, because with this method, the fact that I didn't include it is enough to get it deleted. Think of the list of things on the left of the tr/// operator as the list of what to keep, because we used the /c modifier. Everything else gets deleted, because we used the /d modifier.
Updated: Added the '.' character to the "keep these" character list to accommodate the fact tht the OP revised his question later in the thread asking to also preserve the dot character.
|