A lot depends on what you want to "clean." For the specific case you mention (with all those naughty barewords... put on some quotes, or you'll catch a horrible illness), you might be able to employ the builtin character class \w ("word" characters) or its negation, \W (not "word" characters). The perldoc perlre can help a lot here. Since I can't tell exactly what you're trying to fix, I'll assume you want to get rid of any non-word characters at the end of the string, for which
$directory =~ s/\W+$//;
ought to suffice. The $ anchors the match to the end of the string, and "\W+" matches "one or more non-word characters". It then replaces any such pattern found with nothing.
I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche