in reply to getting a while loop to terminate

You're indentation is all over the place. If we fix that and analyze just your misbehaving subroutine:

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" } }
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.

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:

Replies are listed 'Best First'.
Re^2: getting a while loop to terminate
by Anonymous Monk on Apr 15, 2012 at 07:26 UTC

    Another reason to avoid dirname is because that function does more than return a name, it makes directories

    I choose dirpp because its sufficiently close to dirname but sufficiently inane :) The way I say it, its der-pup/der-poop/der-pee-pee. I suppose the PP could be interpreted as ++ (plus plus )

    I also suppose MakeIncDir / MakeSeqDir / NextDir could work as well.