A: 
# Takes the name of a file and returns its entire contents as a string.
sub getfile 
{
  my($filename) = @_;
  my($result);

  open(F, $filename) or die "OPENING $filename: $!\n";
  while(<F>) { $result .= $_; }
  close(F);

  return $result;
}
dreeves
I thought I had checked through all the answers that there was no while (<FILE>) {$result .= $_} answer before I posted, I can't imagine how I missed this one. Silly me.
Oh, I originally had it as while($line = <F>) { $result .= $line; } for some reason. So you're excused for missing it! :)
dreeves
+16  A: 
open(my $f, '<', $filename) or die "OPENING $filename: $!\n";
$string = do { local($/); <$f> };
close($f);
dreeves
please use 3 parameter open()
szabgab
+47  A: 

How about this:

use File::Slurp;
my $text = read_file($filename);
Leon Timmermans
What if you don't want this to die if the file doesn't exist?
dreeves
@dreeves: my $text = eval { read_file $filename }; or see http://search.cpan.org/perldoc?File::Slurp#err_mode
Sinan Ünür
The easiest way to prevent that from being likely is that simply first checking if the file exists...
Leon Timmermans
this does have the disadvantage that it is not included in out-of-the-box perl. at least not my ActiveState perl for windows (v5.10.0).
Kip
+4  A: 
{
  open F, $filename or die "Can't read $filename: $!";
  local $/;  # enable slurp mode, locally.
  $file = <F>;
  close F;
}
zigdon
A: 

Candidate for the worst way to do it! (See comment.)

open(F, $filename) or die "OPENING $filename: $!\n";
@lines = <F>;
close(F);
$string = join('', @lines);
dreeves
This is my preferred method.
Paul Nathan
This is probably the most inefficient way I can think of, especially for large files. Now you have two copies of the same data and you have processed it twice just to load it into a scalar.
Robert Gamble
It's all situational. For a small file or a run-only-once quickie script, where "$string=`cat $filename`" is not available, this is perfectly reasonable. Inefficient yes! But that's not necessarily the only consideration.
Mr.Ree
+3  A: 

See the summary of Perl6::Slurp which is incredibly flexible and generally does the right thing with very little effort.

mopoke
+18  A: 

I like doing this with a do block in which I localize @ARGV so I can use the diamond operator to do the file magic for me.

 my $contents = do { local $/; local @ARGV = $file; <> };

If you need this to be a bit more robust, you can easily turn this into a subroutine.

If you need something really robust that handles all sorts of special cases, use File::Slurp. Even if you aren't going to use it, take a look at the source to see all the wacky situations it has to handle.

brian d foy
I'm lazy and write `my $contents = do {local (@ARGV,$/) = $file; <>};`, which is the exact same thing in less characters :)
ephemient
I'm wondering why local @ARGV = $file; <> would be any different than <$file>.
R. Bemrose
@Bemrose: because $file is not a filehandle.
brian d foy
+8  A: 

Things to think about (especially when compared with other solutions):

  1. Lexical filehandles
  2. Reduce scope
  3. Reduce magic

So I get:

my $contents = do {
  local $/;
  open my $fh, $filename or die "Can't open $filename: $!";
  <$fh>
};

I'm not a big fan of magic <> except when actually using magic <>. Instead of faking it out, why not just use the open call directly? It's not much more work, and is explicit. (True magic <>, especially when handling "-", is far more work to perfectly emulate, but we aren't using it here anyway.)

Tanktalus
And in case it's not obvious to those following along at home, at the end of the curly block, $fh goes out of scope and the file handle is closed automatically.
dland
+7  A: 
snoopy
+4  A: 

This is neither fast, nor platform independent, and really evil, but it's short (and I've seen this in Larry Wall's code ;-):

 my $contents = `cat $file`;

Kids, don't do that at home ;-).

moritz
as long as you aren't running it on windows..
Kip
+26  A: 

In writing File::Slurp (which is the best way), Uri Guttman did a lot of research in the many ways of slurping and which is most efficient. He wrote down his findings here and incorporated them info File::Slurp.

Schwern
+2  A: 
use Path::Class;
file('/some/path')->slurp;
+2  A: 

Here is a nice comparison of the most popular ways to do it:

http://poundcomment.wordpress.com/2009/08/02/perl-read-entire-file/

dreeves