Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

When you need to process small files, it's usually easier to read the whole file contents in a variable; here's how to do it:

-- Read the contents into an array

Each row will be stored in an array element:

open FILE, "<file.txt";
@lines = <FILE>;

-- Read the contents into a scalar

The whole file is stored in a single scalar variable. To do this, the special variable $/ should have an undefined value when reading the file.

Here's one way to do it:

open FILE, "<file.txt";
$file_contents = do { local $/; <FILE> };