[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:

> 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++;
>    }
> 
> 
> HINT below

>From 'perlop':

       The auto-increment operator has a little extra builtin magic
       to it.  [1] If you increment a variable that is numeric, or
       that has ever been used in a numeric context, you get a normal
       increment.  [2] If, however, the variable has been used in only
       string contexts since it was set, and has a value that is not
       the empty string and matches the pattern /^[a- zA-Z]*[0-9]*$/,
       the increment is done as a string, preserving each character
       within its range, with carry:

The above $target is neither numeric nor matches it the pattern above,
so it evaluates to 'undef' _after_ one correct line is printed.


This is my solution:

   # find a suitable temporary name
   $target = "$TMPDIR/quickblast${$}";
   $suff = 'aaa';
   my $tries = 100;
   while (--$tries) {
     last if IO::File->new($target.$suff,O_WRONLY|O_EXCL|O_CREAT,0600);
     $suff++;
   }

Here critera [2] matches and all is as expected.

  -- Holger


-- 
+ PGP || GnuPG key -> finger eitz@jonathan.weh.rwth-aachen.de +
+++ Debian/GNU Linux <octavian@debian.org> +++ ICQ: 2882018 +++

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