|Welcome to the Monastery|
lets see what official perl documentation has to say about the read() function:
Reads length bytes from file handle into variable (starting at offset if specified). It returns the number of bytes actually read.
bah.. i don't understand that!!
ok.. so here's the deal with read()... what it does is read a FILEHANDLE in chunks instead of reading it in whole. What you might usually do is open a file and dump it into an array, but what if that file was incredibly huge? Well then, you'd have a very large array, and with that large array, you'll be taking up lots of space, sometimes so much that what you want to do won't actually happen. That is why the read function is useful. ok.. so you have that little bit of information, now how can you use it.
A function like this serves almost no better use that to copy one or more files (well at least for me). what you don't wanna do with a function like this is read a database file for example. That is because you might read a certain amount of the file, and cut halfway between one element of the database (which would just be screwey).
ok.. just to remind you... we are doing this to save massive amounts of memory.
the syntax for the read() function is as follows:
lol... cheap syntax...
here's the code anyhow:
read() can help you when your just printing the contents of a big file:
take into account with this example that i did a few things wrong (just to generalize the code). I did not check for errors anywhere in the code, i did not flock my files (unix), and i certainly did not binmode() my files (in case i was on windows).
Q: why didn't i open,close, then read the original file?
A: can't read a closed filehandle silly!!
Q: why did i read 1024, what's up with that number?
A: it's a magical number!!!!!!!!!, wait, no no... when you read a file, you read it in bytes (every character = 1 byte)... so why again am i reading in 1024 bytes? Well that's cuz 1024 bytes = 1 kilobyte, i'm just being consistant here... you can actually read in any number you want.