use Path::Tiny; my $str1 = "/fold1/fold2/fold3/file.pl"; print path($str1)->dirname, "\n";