I might approach the problem slightly differently, with an inverted character class:
c:\@Work\Perl\monks>perl -wMstrict -le
"use constant NOT_ALLOWED => qr{ [^-\x27\x23\$\@0-9a-zA-Z.\\/_%(){}&!~
+`^] }xms;
;;
for my $str ('', qw(ABC_ABC 'A' ///?/// =*+| ABC=), ' ', qq{\t
+\r\n}) {
my $status = 0 + $str =~ NOT_ALLOWED;
print qq{[$str] $status};
}
"
[] 0
[ABC_ABC] 0
['A'] 0
[///?///] 1
[=*+|] 1
[ABC=] 1
[ ] 1
[
] 1
Note that:
-
A ^ (caret) in the first position in a character class inverts the contents of the class.
-
A - (hyphen) in the first position in a character class (or in the first position after an initial caret, if there is one), or in the last position in the class is a literal hyphen character. Everywhere else it is a character range operator as in A-Z and 0-9 elsewhere in the example class.
-
The $ @ characters should always be escaped, as otherwise they will often be interpreted as interpolable variables.
-
Finally, \x27 \x23 are how I have to represent the ' # characters, respectively, because my REPL doesn't like these literal characters in qr// and similar operators. (Have to fix that someday.)
(And a lot more test cases wouldn't be a bad idea.)
Give a man a fish: <%-{-{-{-<