sub dir_ensure { for (@_) { # handles multiple DIRs my $path = ($_ // die("Dir must be specified")) =~ s!/$!!sr; next if -d $path; # DIR is already there die("Path '$path' already exists and it's not a DIR") if -e _; my $parent = $path =~ m"^(/[^/].*?)/(?!\.{1,2}$)[^/]+$"s ? $1 : die("Invalid path '$path'"); dir_ensure($parent); # ensure parent DIR exists mkdir($path) or die("Failed to create DIR $path $!"); } }