Perl – How to create a directory if one doesn’t exist using Perl


Currently, my Perl output is hard-coded to dump into the following Unix directory:

my $stat_dir = "/home/courses/" . **NEED DIR VAR HERE**;

The filename is built as such:

$stat_file = $stat_dir . "/" . $sess.substr($yr, 2, 2) . "_COURSES.csv";

I need a similar approach to building Unix directories, but I need to check if they exist first before creating them.

How can I do auto-numbering (revisions) of the $stat_file so that when these files get pumped into the same directory, they do not overwrite or append to existing files in the directory?

Best Solution

Erm... mkdir $stat_dir unless -d $stat_dir?

It really doesn't seem like a good idea to embed 'extra' questions like that.