Peter Stuifzand

Create sub directories in Perl

Sometimes you need to create a large tree of subdirectories. But why? Two examples that I think of are structured directories for weblogs, e.g. /[year]/[month]/[day]/, or the automatic backing up of files e.g. invoices/[company]/[year].

Before you begin, you know that using a split and chdir, or some other combination of built-ins, will just make big mess. A call to mkdir -p could also work, but let’s use the available modules this time.

If you use Perl there is always the CPAN that can help you. So, also this time. Enter File::Path.

use File::Path 'make_path';
make_path('posts/2011/04/04');

This will create this structure below the current directory. Simple. There are many of these modules hidden (or less hidden) in the CPAN. THey like the light.

© 2023 Peter Stuifzand