in reply to How do I read an entire file into a variable?

$/=undef; #sets the record separator to [undef] so i +t keeps reading #until there is nothing more to read open FILE, "<yourfile"; my $data=<FILE>; close FILE; $/="\n"; #set it back to \n so if you try to read f +rom the #terminal it only takes a line at a time

Replies are listed 'Best First'.
RE: RE: How do I read an entire file into a variable?
by Kasei (Novice) on Dec 29, 1999 at 06:08 UTC
    Probably better to use local() and a block so as not to cause problems if somebody else set $/ to something other than \n (assumptions can come back to haunt you) .
    my $data; { local($/) = undef; open (FILE, "<yourfile"); $data = <FILE>; close FILE; }
      Good point... although I think now that I see nate's method I think that will be my method of choice in the future.