Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

In reply to read() by Parham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-24 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found