#!/usr/bin/env perl use strict; use warnings; use 5.10.0; use POSIX qw(strftime); use File::Basename; my $root; if (defined $ENV{P1K3_ROOT}) { $root = $ENV{P1K3_ROOT} . '/archives'; } else { # Fall back to a default: $root = $ENV{HOME} . '/workspace/p1k3/archives'; } # I used to think strftime didn't have a non-zero-padded month/day; # it turns out you can do this with a - after the % (this may not # work everywhere, so my original workaround is commented below): my $dayfile = strftime("%Y/%-m/%-e", localtime()); # $dayfile =~ s{/[0 ]}{/}gx; # # (Or: Use it anyway and apply an ad-hoc regex fix, because though we often # feel we know better in this day and age, we are not ashamed of text # transformation. not when the chips are down. text is the most powerful # computational abstraction i've ever come close to understanding, if i'm # honest, and past a certain point life is too short and too precious a span to # abandon the only tools you have ready to hand.) my $today = "$root/$dayfile"; if (-d $today) { print $today; } else { my $dirname = $today; until (-d ($dirname)) { # If a dir for the month or year doesn't exist, this moves one step up. # Should land in archives/ if no year. $dirname = dirname($dirname); } print $dirname; }