Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
barring any syntax errors:
use Fcntl qw(:DEFAULT :flock); open (MYFILE1, "<", "$file") or sysopen (MYFILE, "$file", O_RDONLY) or die "$!"; flock (MYFILE1, LOCK_SH) or die "$!"; chomp (@input=<MYFILE>); close MYFILE1; # pretending you then did your work on the input, split at the ":" # and stuck the results back into @output... open (MYFILE2, ">", "$file") or sysopen (MYFILE, "$file", O_WRONLY) or die "$!"; flock (MYFILE2, LOCK_EX) or die "$!"; foreach $item(@output){ print MYFILE2 "$item"."\n";} close MYFILE2;

doing the double open-sysopen technique helps avoid races for opening the file and having that content altered in those few miliseconds.

it's rare, if you have a small site, that two users would access the file at the same time - but i wouldn't take the risk. Besides, w/CGI work - if someone is ~intentionally~ messing w/your system -> you want to ensure that you're opening the right data and not something that was swapped in by a nefarious user. (This is the reason for using 2 separate filehandles too -> MYFILE1 and MYFILE2).

LOCK_EX (exclusive locks) are needed, when writing to files - otherwise, LOCK_SH (shared locks) are all that's needed, i believe, when reading.

also: i'm not sure, if i'd actually use DIE like this in a cgi system, if the user is going to see the output. You probably want to 'die' gracefully -> using WARN, &subroutine, or some other action instead.

for more info - see Camel -> pgs 419-422, 571-573, 712, 714-715, 808-810


In reply to Re: How to modify/delete records in a flat-file database? by wolfi
in thread How to modify/delete records in a flat-file database? by JoeJaz

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? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-12-08 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?