in reply to Overwrite file protection

Hello,

I don't see the point in using '<' and '>' for the filename, inside the for loop.

This do the trick:

if( -e $upload_dir.'/'.$filename2 ){ my $inc = '1'; while( -e $upload_dir . '/' . $filename2 . '~' .$inc ){ $inc++; } # Use "${upload_dir}/${filename2}~${inc}" as the filename. }

Warning: untested code. Please pay attention to possible mispells and errors.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Just Another Perl Monk

Replies are listed 'Best First'.
Re: Re: Overwrite file protection
by Willman023 (Scribe) on Jun 16, 2003 at 20:22 UTC
    It's funny you guys recommended using a while loop, as thats originally what I did in fact use. I'm assuming that when I had: -e <$upload_dir/$filename2 . "~" . $inc>; compared to your -e $upload_dir . '/' . $filename2 . '~' . $inc that it was possibly not matching the appended string correctly, and that may be where my problem was. But, after pasting in monsieur's code I was in fact incrementing correctly, Thanks Alot guys!

    bW

    Perl - the breakfast of Champions!

      I'm sorry. I should explain what's going on behind the scene:

      When you surround your filename expression (actually, a scalar holding the filename) with '<' and '>', what you get is a file glob. That's the name of this use of '<' and '>' surrounding a scalar designing a filename or something that looks like a file name set (think on it as the equivalent of using dir *.txt on the old D.O.S.).

      File globbing is used to do strange (not that strange, indeed) things with sets of filenames.

      The strange behavior of your script was because the file name globbing was working when you didin't like it to.

      Maybe I should rewrite this comment, I feel that it may confuse you. If it do so, please let me know and I will try to fix it up

      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Just Another Perl Monk

        I vote for a more detailed version of the post, Monsieur. I'd be interested in the strangeness, and I've never done fileglobbing before.


        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D