Chris wrote:
> 
> On Thu, 24 Oct 2002 00:03:48 -0700, [EMAIL PROTECTED] (John W. Krahn)
> wrote:
> 
> >You mean something like this:
> >
> >my ( $file, $ext );
> >do  {
> >    $file = sprintf 'test.%03d', ++$ext;
> >    } while -e $file;
> >
> >open OUT, "> $file" or die "Cannot open $file: $!";
> 
> Exactly. Thank you.
> 
> I received so many good suggestions. Thanks to all.
> 
> my $testfile = 'test.txt';
> my ( $file, $ext );
> if (-e $testfile){
>         do  {
>             $file = sprintf 'test.%03d', ++$ext;
>             } while -e $file;
>         rename ($testfile, $file);
> }
> 
> open $fh, "> $testfile" or die "Cannot open $file: $!";


You don't really need the do/while loop in this case and you should
include an error message if the rename fails.  Also, you could limit the
scope on the temporary variables.

my $testfile = 'test.txt';

{ my ( $file, $ext ) = $testfile;
if ( -e $file ) {
    $file = sprintf 'test.%03d', ++$ext while -e $file;
    rename $testfile, $file or die "Cannot rename $testfile: $!";
    }
}

open my $fh, '>', $testfile or die "Cannot open $testfile: $!";



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to