You're indentation is all over the place. If we fix that and analyze just your misbehaving subroutine:
we see that Perl helpfully warns us that the $name; line above is useless. If we fix that by replacing the useless $name with return $name and change -e -d $name to -e $name and eliminate the now unnecessary $flag we will have a non-looping subroutine that kinda works. Note however that aaron_baugher has already shown us a much cleaner way to code your subroutine and his version is preferred to patching your original code.sub dirname { my $word = "site"; my $counter = 1; my $flag = 1; while ($flag) { my $name = "$word" . "_" . "$counter"; if ( -e -d $name) { $counter++; next; } else { mkdir $name, 0755 or warn "Cannot make dir $name: $!"; $flag = '0'; } print "$name \n"; $name; # "Useless use of private variable in void context" } }
Having said that, this sort of code is tricky to get right, and race conditions and security exploits abound -- which is why Perl provides the File::Temp module. If you tell us your requirements for these "temporary" directories (and files?) we could offer more advice on this and I suspect the core File::Temp module could be employed to satisfy your requirements.
Other random stylistic suggestions:
In reply to Re: getting a while loop to terminate (File::Temp)
by eyepopslikeamosquito
in thread getting a while loop to terminate
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |