Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a specific explanation of +<, +>, >> and +>> 1

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
I am reading up on using FILEHANDLES, and they don't (for my level of understanding) fully explain the what, why, how, when and specific differences between the following.

+<

+>

>>

+>>


1) Are these considered OPERATORS or what?

2) They said something to the regard of &quot;To open a file for eading and writing without truncating the file, prefix the file's name with a plus sign and a less than sign (+<), as follows.

open (RWFILE, &quot;+<mytext.txt&quot;);

I don't understand what they mean by TRUNCATING in this example? What does truncating mean in this situation, and how is tuncating used?

3) How are these THINGIES (or operators - of what ever they might be called) specifically used and how?

Is there anything I need to know about them?

Thanks,
Gary

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
okay, file opening lesson.

when you open a file, you usually only open it with one thing in mind. either you're going to read data from it, you're going to create a new file, or you're going to add data to an existing file...
< is for reading.
> is for writing a new file. it erases all the contents of (truncates) any existing file with the same name.
>> is for appending, adding data to a file. However, you can't add data to the middle of the file without overwriting the data that's already there. it doesn't work like a word processor.
These aren't operators at all, simply characters that perl looks for at the beginning of a filename it's opening. if there are none, it assumes it is opening the file to be read (<).
If you need to open a file for both reading and writing, you simply add a + sign in front of the other symbol at the front of the filename. however, the open function will still perform all the basic operations of the other symbol, such as truncating in the case of >, which would effectively destroy all the data in the file, thus making it (usually) a pointless effort. you could, of course, still read the data you write to the file as you're doing it, just none of what used to be there.

however, a note on reading and writing simultaneously (you don't have to read this next paragraph in order to do basic stuff, just if you wanna do reading and writing at the same time.). when doing either, perl has to remember where you are in the file. if you're writing to it, you're usually at the very end. if you're reading from it, you start at the beginning, and move usually one line at a time. you can find out where you are by using the tell fuction in perl. this gives the position in the file as a number of bytes from the beginning of the file. there are a couple of different ways to change that position. first, if you read from the file, the position marker will move ahead as far as you read. second, you can use the seek function in perl. the syntax is that you supply it a filehandle and two integers. the first integer is named 'offset'. it is the offset in bytes, and can be positive or negative. the second number is named 'whence', and it determines where the offset is taken from. it can only have 3 values, 0, 1, or 2. 0 means to start at the beginnig of the file, and this is the assumed value if you leave it off. 2 is the end of the file. 1 is the position of the cursor before the seek was called, referred to as 'current position'. as was mentioned before, when opening a file for both reading and writing, the starting position will depend on how you open it. reading starts at the top, appending starts at the bottom, and, well, the other case doesn't have much variety of position to be in. in the case of a file having reads and writes done to it in succession, the position marker will likely have to be changed repeatedly in order for any sensible work to be done. if you read a couple lines, then write a couple, then read some more, what just happened is that the data inbetween the two reads will be replaced by what is written. you'll have to tell and seek over and over in order for read/write filehandles to work correctly.

i hope that was all at least somewhat understandable. if not, leave off that last paragraph, and then try to understand it again. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thanks.

So ... if you use +< to read and write, ... correct me if I'm wrong, but since this is the READ character, it won't erase the data in the file, yet .. it should or would give me the ability to write to it as well as read it?

Is this correct?

But, from what you said, .. if I used +> ... that would potentially create a problem - if I was wanting to read (what was there before) and be able to write to the file. Since you said, (as I understood it) that since it is using the > character ... even though I added the + in front of it ... the > is still going to (truncate) erase all the previous data from the file.

Likewise, if I used, based on what you said ... the +>> ... this should allow me to READ and WRITE (to the end of the file data)?

Did I understand you correctly.

THANKS AGAIN!!!
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
good job. :)I &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top