[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Puzzle - Temp file creation



On Mon, Jul 12, 1999 at 02:02:00PM -0700, Vicki Brown wrote:
> This has the outward appearance of attempting to make a temporary file
> 
> What is wrong with this code?
>    # find a suitable temporary name
>    $target = "$TMPDIR/quickblast${$}aaaa";
>    my $tries = 100;
>    while (--$tries) {
>      last if IO::File->new($target,O_WRONLY|O_EXCL|O_CREAT,0600);
>      $target++;
>    }

First, it's ridiculous. What about 

	$target=`mktemp $TMPDIR/quickblast.XXXXXX`;

(I'm assuming there isn't a Perl module already to do this, which there
probably is.) Second, since when has "aaaa"++ been "aaab"?

	azz@cartman$ perl
	$a = "aaaa"; print $a; print $a++;
	aaaaaaaa

Also, it won't do the right thing if it fails, since it'll end up at the
next statement after the } whatever it does. You probably want

	die "couldn't get a random filename" if !$tries;

after the }.

-- 

Adam Sampson
azz@gnu.org

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe