I don't understand the code above, but if it's only substituting the underscore for a hyphen should it be this instead:
$name =~ s/[^\w\d_\-\/]/-/g;
In Alex's example a \- seems to have disappeared?
$name =~ s/[^\w\d_\-\/]/-/g;
In Alex's example a \- seems to have disappeared?