for your comments:
* removed the comments about backticks and cat. not everyone has cat.
* added an example that uses read().
Index: perlfaq5.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
retrieving revision 1.12
diff -u -d -r1.12 perlfaq5.pod
--- perlfaq5.pod 11 Mar 2002 22:25:25 -0000 1.12
+++ perlfaq5.pod 12 Mar 2002 15:36:59 -0000
@@ -764,21 +764,7 @@
which allow you to tie an array to a file so that accessing an element
the array actually accesses the corresponding line in the file.
-On very rare occasion, you may have an algorithm that demands that
-the entire file be in memory at once as one scalar. The simplest solution
-to that is
-
- $var = `cat $file`;
-
-Being in scalar context, you get the whole thing. In list context,
-you'd get a list of all the lines:
-
- @lines = `cat $file`;
-
-This tiny but expedient solution is neat, clean, and portable to
-all systems on which decent tools have been installed. For those
-who prefer not to use the toolbox, you can of course read the file
-manually, although this makes for more complicated code.
+You can read the entire filehandle contents into a scalar.
{
local(*INPUT, $/);
@@ -790,6 +776,13 @@
close the file at block exit. If the file is already open, just use this:
$var = do { local $/; <INPUT> };
+
+For ordinary files you can also use the read function.
+
+ read( INPUT, $var, -s INPUT );
+
+The third argument tests the byte size of the data on the INPUT filehandle
+and reads that many bytes into the buffer $var.
=head2 How can I read in a file by paragraphs?