Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Keep It Simple, Stupid
 
PerlMonks  

read()

by Parham (Friar)
on Dec 31, 2001 at 07:01 UTC ( #135323=perltutorial: print w/ replies, xml ) Need Help??

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:

read(FILEHANDLE,$into_a_variable,$which_is_certain_bytes);

lol... cheap syntax...

here's the code anyhow:

open (FILE1, "$original"); open (FILE2, ">$copy"); while ( read(FILE1,$file_contents,1024) ) { print FILE2 $file_contents; } close (FILE1); close (FILE2);
read() can help you when your just printing the contents of a big file:

open (FILE1, "$original"); while ( read(FILE1,$file_contents,1024) ) { print "$file_contents"; } close (FILE1);
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).

followup questions:
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.

Comment on read()
Select or Download Code
Re: read()
by archen (Pilgrim) on Dec 31, 2001 at 20:24 UTC

    What you say is all true when dealing with text files, but when it comes to dealing with non text files (like an mp3, or jpeg), read() is probably going to be your only solution since you really can't read those types of files line by line, and who knows what you'll get in an array. The usefulness of read is best understood when you understand it's compliment function seek() (well to me anyway). With these two functions you can jump around a file and read various amounts of data, without worrying about hitting an unexpected \n.

      Why can't you `my @data = <FILEHANDLE>;` for binary data? It won't exactly be logically split, but `my $r = join '', @data` works perfectly fine.... Of course, I wouldn't want a huge file in memory...
        This way you effectively pwn all of the chr(10) in your binary file... which is bad. The better "slurping" solution is to undef$/ and then using <>;
Re: read()
by larryk (Friar) on Jan 01, 2002 at 02:47 UTC
    another way to read in "chunks" is to set $/ = \number and then read from the filehandle as you would normally, e.g.
    open FILE, $path; $/ = \1024; print while <FILE>;
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://135323]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-04-16 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls