Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Now, first of all, from this point on, I can quite willing to be corrected, however to my mind in order to prevent a race condition, the very next operation which should be carried out following the determination of the file name is the acquisition of an exclusive lock on the file. While your code iterates through the loop until a non-existent file name is determined, to my thinking, the next operation must be to open the file and acquire the lock rather than the test for existent (which should fail, thereby exiting the loop and allowing the file to subsequently opened) - The difference in code is minute and frankly I don't really know if its absolutely necessary, however, if I were requested to rewrite the code, the following is how I would rewrite it:

(Note that this philosophy with regard to order of actions to prevent race conditions with temporary files is based upon discussions within the comp.lang.perl.moderated newsgroup thread here and a BUGTRAQ post from Tom Christiansen here)

#!/usr/bin/perl use Fcntl; use strict; my $fname = nextunique( 'aaa', [ map { m/tmp_(\D+).txt$/ } glob('./tmp +_*.txt') ] ); do { $fname = nextunique( $fname, [ map { m/tmp_(\D+).txt$/ } glob('./t +mp_*.txt') ] ); } until open (FH, $fname, O_RDWR|O_CREAT|O_EXCL, 0666); # code follows close FH; sub nextunique { my ($test_item, $item_list) = @_; exit 1 unless UNIVERSAL::isa( $item_list, 'ARRAY' ); foreach ( @{ $item_list } ) { if ( $_ eq $test_item ) { ++$test_item; last; } } return $test_item; }

You will also note that I have removed the while loop within the nextunique subroutine, thereby minimising the chance of an infinite loop should the element $new_item not exist within @{ $item_list }. A more defensive approach to programming? Maybe, maybe not ...

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'


In reply to Re: Re: get new item name by rob_au
in thread get new item name by mkmcconn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found