in reply to popping or shifting a line off a file
I assume you are going to run this program relatively often, other wise you should be spending your time on other things than efficiency. However, if this is going to be run often, you want to avoid deleting parts of the file as much as possible. I do not recommend Tie::File solutions, as they will modify the file.
Instead, I recommend changing the content of the file. Instead of having 8 characters on a line, have nine. The ninth character will indicate whether a record has been used or not. Say you use a ! and a * for the ninth character. Initially, all records will have a !, signalling it's unused. Once you use a record, flip the ninth character to be a *.
You do have to flock your file though, otherwise two instances of your program could hand out the same password. And that's something you want to avoid. Also make sure that you first mark a record for deletion before you actually hand it out. Because in that case, the worst that can happen is some records will be marked as 'in use' while they aren't. Otherwise, an unfortunate crash or termination of your program might cause a record to be reused. (Too bad Perl flock() can only lock entire files, not regions).
I do think however that you will be better off not using a file based system. Using a good database will solve many of your problems with concurrent access and efficient deletion of records.
Abigail
|
|---|