Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

help with SEEK, TELL, READ, others

by Kozz (Friar)
on Jun 29, 2000 at 04:55 UTC ( [id://20308]=perlquestion: print w/replies, xml ) Need Help??

Kozz has asked for the wisdom of the Perl Monks concerning the following question:

Most wise monks:

I'm having some difficulties. I want to open a file for read/write and immediately obtain an exclusive lock, read the whole thing into a scalar (or array), do something with the data I read, then write it all back to the file (clobbering whatever was once there), then close it. But how do I do all this? I thought I saw a good explanation in a post a week ago, but I couldn't find the post. And I also tried reading the manpages, but was unable to decipher all the functions. Perhaps I'm missing something.
Here's what I've got:
open(FH, "+< filename.txt") || die "$!"; #open the file for read/writ +e flock(FH, LOCK_EX); # get an exclusive lock seek(FH, 0, SEEK_SET); # move filepointer to beginning of file read(FH, $scalar, $bytes); # what if I want the whole thing, no matter + how many bytes? # do something with # $scalar here seek(FH, 0, SEEK_SET); # move filepointer to beginning of file (is + this correct syntax for this?) print FH $scalar; # print at beginning of file, clobbering ol +d data truncate(FH, tell(FH)); # truncate any data beyond where we've writ +ten (is this correct as well?) close(FH); # close the file
So the problem is that this seems to leave me with an empty file, rather than reading and then writing what I had. Perhaps I'm intermixing some seek/read/truncate calls incorrectly or something. In addition, if I simply want to read the entire file into the scalar using read(), how would I do that, since I don't know the length (in bytes) to ask for? Honestly, I did read some man pages for read, seek, truncate, tell, but I must be missing something, or making a blatantly obvious mistake.

Any help you can provide is much appreciated.
Thanks! --Kozz

Replies are listed 'Best First'.
Re: help with SEEK
by Ovid (Cardinal) on Jun 29, 2000 at 06:03 UTC
    Kozz, the post you were looking for might be here. Not trying to blow my own horn, just thought it answered what you were asking.

    Of course, it just covers the seek, truncate, and tell portions of your question. If you need more info about flocking, it doesn't address that.

    As a side note: always keep in mind that using flock is only a polite way of asking others to stay out. If they don't check, flock won't help. I'm sure you know that, but I think it's always safe to mention that.

    Cheers!

Re: help with SEEK
by takshaka (Friar) on Jun 29, 2000 at 10:33 UTC
    In addition, if I simply want to read the entire file into the scalar using read(), how would I do that, since I don't know the length (in bytes) to ask for?
    my $buf; $scalar .= $buf while read(FH, $buf, 4096);
    or
    my $size = (stat FH)[7]; read(FH, $scalar, $size);
    As long as all processes honor your flock, there shouldn't be a race condition with the stat + read, but it still makes me uncomfortable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://20308]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found