use File::Path 'make_path'; $path = '/'; File::Path->make_path($path); chdir $path or die "Cannot chdir: $!";