For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > January 2005 > Read Write to file .. need help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Read Write to file .. need help
Luinrandir Hernsen

2005-01-14, 8:55 pm

I need to read and write to a file before allowing the anyone else to read
or write.
I found this on the web.. but it does not work...maybe its my version of
perl?

http://htmlfixit.com/cgi-tutes/ tut...n_with_Perl.php
File Operation modes:
open:
<$file&path Read access only (The same as not specifying a mode.)
>$file&path Write access, create if nonexistant, and overwrite existing

data.[color=darkred]
+<$file&path Read and Write only, no file creation/appending. Overwrite
existing data. <<<<<<<<<<< I tried this one
+>$file&path Read, Write, Create, overwrites rather then appending.
+>>$file&path Read, Write, Create, Append, no overwriting.
| SYSCOMMAND Write data to external command only.
SYSCOMAND | Read data from external command only.

Anyone know about this? or can I just:
#Open to read
open (DataFile, "$_[0]")||die "Sorry, I can't open $_[0]\n";
flock (DataFile,2);
@Data =<DataFile>;
#open to write
open DataFile, ">$_[0]")||DieNice3("Can't write to $_[0]");
flock (DataFile,2);
foreach $Data(@Data)
{
print DataFile "$Data\n";
}
close (DataFile)||die "Sorry, I can't close $_[0]\n";

Ovid

2005-01-14, 8:55 pm

Hi Luinrandir,

> Anyone know about this? or can I just:
> #Open to read
> open (DataFile, "$_[0]")||die "Sorry, I can't open $_[0]\n";
> flock (DataFile,2);
> @Data =<DataFile>;
> #open to write
> open DataFile, ">$_[0]")||DieNice3("Can't write to $_[0]");
> flock (DataFile,2);
> foreach $Data(@Data)
> {
> print DataFile "$Data\n";
> }
> close (DataFile)||die "Sorry, I can't close $_[0]\n";



Reading and writing files from CGI scripts can be tricky due to "race
conditions". A race condition occurs when more than one process is
trying to do something but the data corruption can occur if they do
those things in the wrong order. So let us say you want to

1. open a file
2. flock it
3. write to it
4. close it

Now let's say another process tries to read that file but does so after
the first process executes step one and before it executes step two.
The second process will be trying to read an apparently empty file.
"sysopen' actually allows you to open and lock the file at the same
time, but this is not supported on all systems.

For a clean way of handling this by using what are known as "semaphore"
files, read "Resource locking with semaphore files"
(http://interglacial.com/~sburke/tpj/as_html/tpj23.html)

Cheers,
Ovid


=====
Silence is Evil http://users.easystreet.com/ovid/ph...hy/decency.html
Ovid http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl http://users.easystreet.com/ovid/cgi_course/
Graeme St. Clair

2005-01-15, 3:55 am

Oddly enough, I was improving the shining hour in a hospital waiting room
earlier today by reading the camel book Chapter 23 Security. There is a
good discussion of this whole topic, bearing very exactly on what Luinrandir
is trying to do.

L, if you haven't already got this book "Programming Perl" by Wall et al,
published by O'Reilly, you should! If you're a total beginner, first read
the llama book, O'Reilly again, "Learning Perl" by Schwartz, who
occasionally posts to this list as "Merlyn".

Neither of these books are ever more than 6 feet (2m!) away from my desk,
except when I'm reading them in a more comfortable chair...

Rgds, GStC.


-----Original Message-----
From: Ovid [mailto:publiustemp-beginnerscgi@yahoo.com]
Sent: Friday, January 14, 2005 2:47 PM
To: Luinrandir Hernsen; beginners-cgi@perl.org
Subject: Re: Read Write to file .. need help

<snip/>

Reading and writing files from CGI scripts can be tricky due to "race
conditions". A race condition occurs when more than one process is trying
to do something but the data corruption can occur if they do those things in
the wrong order. So let us say you want to

1. open a file
2. flock it
3. write to it
4. close it

Now let's say another process tries to read that file but does so after the
first process executes step one and before it executes step two.
The second process will be trying to read an apparently empty file.
"sysopen' actually allows you to open and lock the file at the same time,
but this is not supported on all systems.

For a clean way of handling this by using what are known as "semaphore"
files, read "Resource locking with semaphore files"
(http://interglacial.com/~sburke/tpj/as_html/tpj23.html)

Cheers,
Ovid


=====
Silence is Evil
http://users.easystreet.com/ovid/ph...hy/decency.html
Ovid http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl http://users.easystreet.com/ovid/cgi_course/

--
To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
For additional commands, e-mail: beginners-cgi-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com