That looks pretty good! I'm gratified that you took my suggestion and ran with it. Thanks for sharing it with us! Looking at what you have, I think it could be made a bit more compact.
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( \'' );
}
Note the differences, though (good and bad).
- I use File::Path::mkpath instead of just mkdir. This will try to create every necessary directory right up to the root. Yours won't try to create anything above /my/base/path. If attempting to create directories above /my/base/path will be a problem, you should go back to your own code.
- Instead of tracking $init_fail, I track $init_done. This way the while condition is more natural (and doesn't look infinite), and I get to set it in only one place inside the loop.
- I use Log::Log4perl->init with an empty string instead of an empty file I just created. It's shorter, and I don't run the risk of failing to create the empty file.
- I re-complicated the regular expression (in particular adding the /x form back).
- I haven't tested this. There could even be a syntax error.
Thanks again for posting your work.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.