my $FILE = sub { my $file = shift; if($file =~ m!\A ([A-Za-z]:\\)? # Optional DOS drive (such as 'C:\') ( [/\\]*? # Allow either '/' or '\' as a directory seperator [-\.\w\s]+? # Allow certain characters as the filename )+ \z!x) { return ($1 . $2, "Passed"); } else { return (undef, "$file is not a valid filepath"); } };