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

Re: [FWP] Puzzle - Temp file creation



Quoting Vicki Brown (vlb@cfcl.com):
> 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++;
>    }

*SCHPOILERS!!!*
If you're still trying to figure it out, don't read any further!

















I thought at first it might be something to do with the bizarre use of ${$}
for $$, or the failure to properly check $!==EEXIST, but eventually had to
go to the perlop man page for the answer. I quote:

       The auto-increment operator has a little extra builtin
       magic to it.  If you increment a variable that is numeric,
       or that has ever been used in a numeric context, you get a
       normal increment.  If, however, the variable has been used
       in only string contexts since it was set, and has a value
       that is not null 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 problem becomes obvious: $target doesn't match the regular expression
for magic increment, so ++$target eq "1".

-- 
Adam Rice -- wysiwyg@glympton.airtime.co.uk -- Blackburn, Lancashire, England

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