Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (9)
    As of 2014-08-22 08:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (150 votes), past polls