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.
In reply to Re^7: getting a while loop to terminate
by aaron_baugher
in thread getting a while loop to terminate
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |