Perl – How to Read a Text File into a Variable – 6 ways to do it
6 Ways to Read a Text File into a Variable If you are working with large file(s) you might consider using File::Slurp. It is much fast than the conventional: { local $/=undef; open FILE, "myfile" or die "Couldn't open file: $!"; binmode FILE; $string = <FILE>; close FILE; } { local $/=undef; open FILE, "myfile" or die "Couldn't open file: $!"; $string = <FILE>; close FILE; } open FILE, "myfile" or die "Couldn't open file: $!"; $string = join("", <FILE>); close FILE; open FILE, "myfile" or die "Couldn't open file: $!"; while (<FILE>){ $string .= $_; } close FILE; open( FH, "sample.txt") || die("Error: $!\n"); read(FH, $data, 2000); close FH; The format for the read function is: ...