I've got the following code to assign the contents of a file to an array:
open FILE, "inputfile" or die "Couldn't open file"; @lines=<FILE> close FILE;
Does anyone know how to assign the entire contents of a file to a scalar variable?
sugarkane
12:23 am on Feb 1, 2004 (gmt 0)
open FILE, "inputfile" or die "Couldn't open file"; $content.=<FILE> close FILE;
...will do the trick.
The important part is the .= in the line $content.=<FILE> , which means the next line of the file is appended to $content rather than $content being set to each line in turn.
SlowMove
9:11 pm on Feb 1, 2004 (gmt 0)
Thanks for the help.
moltar
10:01 pm on Feb 1, 2004 (gmt 0)
local (*HANDLE, $/, $_); open(HANDLE, '< file.txt') or die $!; my $string = <HANDLE>; close(HANDLE);
dkubb
4:18 am on Feb 2, 2004 (gmt 0)
Here's my favorite way to "slurp" a file into a scalar:
open FILE, 'inputfile' or die "Could not open file inputfile: $!"; sysread(FILE, my $content, -s FILE); close FILE or die "Could not close file: $!";
SlowMove
4:25 am on Feb 2, 2004 (gmt 0)
I apprecitate all the help. I wound up using some code I found searching usenet. It's really fast.