As
BUU mentioned, the use of
qw seems a little, strange, but it works anyway, it deparses to:
$cwd =~ m[(/.*)/];
But if you don't use it for the sake of obfuscation, I suggest to replace it by something more readable. :)
My guess why your code doesn't work is either
dirname $0 or
$0 is not what you expect. Add some print statements for debugging to see what they contain.
Also, you should check if the regex matches, $1 will only be set if the match is successful:
if ( $cwd =~ m#(/.*)/# ) {
$cwd = $1;
print $cwd;
}