use File::Path qw( mkpath ); my $base_path = '/my/base/path'; my $init_done = 0; my %paths = (); INIT: while ( ! $init_done ) { $init_done = eval { Log::Log4perl->init_once($LOGGER_CONF_FILE); 1 }; # If there's an error, and it's THIS one, if ( $@ =~ m{\A Can't \s open \s \Q$base_path\E (?: / ( \S+ ) )? / [^/]+ \s \( No \s such \s file \s or \s directory \)}xms ) { # This is the path that's missing my $target_path = "$base_path/$1"; # If we've already tried to create it once, don't try again if ( $paths{ $target_path }++ ) { last INIT; } # Try to create the path eval { mkpath( $target_path ) }; if ( ! -d $target_path ) { warn "Can't create missing directory '$target_path': $@\n"; } } } # if we couldn't get the config file to initlize, load a blank one and move on without logging if ( ! $init_done ) { warn "Couldn't initilize logger, defaulting to blank config file"; Log::Log4perl->init( \'' ); }