in reply to Re^6: getting a while loop to terminate
in thread getting a while loop to terminate
After giving it a bit more thought, I realized that the only "acceptable" error from mkdir (the only one you want to continue looping after) is EEXIST (File exists). So maybe it makes more sense to loop on that error. That eliminates the need for any flags, or any extra error checking to avoid a race condition:
$my $counter = 1; use Errno qw[ EEXIST ]; sub mk_new_dir { while(1){ my $name = $word . '_' . $counter++; if( mkdir $name, 0755 ){ return $name; # success, return new dir name } else { next if $!{EEXIST}; # mkdir failed because the file exists; loop + and try next name die $!; # it failed for some other reason; bail out! } } }
Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: getting a while loop to terminate
by Aldebaran (Curate) on Apr 19, 2012 at 02:49 UTC | |
by Anonymous Monk on Apr 19, 2012 at 04:17 UTC |