I'd guess that your web server software doesn't provide the environment variable SCRIPT_FILENAME. That variable is not documented in the CGI 1.1 RFC, so maybe you need to check the documentation of your web server whether they changed their behaviour.
Technically, $ENV{SCRIPT_FILENAME} is supposed to be the name of your program in "file space", as opposed to $ENV{SCRIPT_NAME} which is part of the RFC and is the name of the same program in "URL space". Depending on your web server configuration, you might succeed by simply writing my $dir=dirname($0); because $0 is the file name of your program.