in reply to Overwrite file protection

You have to remember that $inc++ is performed each time round the loop,like so:
$inc = 1; -e < $filename~1 >;$inc++; # file exists, $inc now 2 $inc = 2; -e < $filename~2 >;$inc++; # file doesn't exist, $inc now 3
You may be better off using something like..
$inc = 1; while (-e $filename."~".$inc) { $inc++;}
Cheers, Ben.

Replies are listed 'Best First'.
Re: Re: Overwrite file protection
by sauoq (Abbot) on Jun 16, 2003 at 20:43 UTC

    Or, in a way that reads more naturally (to me, at least)

    my $inc; $inc++ until ( not -e "$filename~$inc" );
    Note that starting with an undefined $inc eliminates the need for the first check. In other words, the whole sub would be as easy as:
    sub good_filename { my $filename = shift; my $inc; $inc++ until ( not -e "$filename~$inc" ); return "$filename~$inc"; }
    -sauoq
    "My two cents aren't worth a dime.";