Tuesday, December 25, 2007

How to read an entire file as a string ? - PERL

Clear $/ to allow the entire file to be read straight into your
variable:

$file = "include.dat";
my $string;
{
undef $/; # read file all at once
open (FILE, $file) die "cannot open $file: $!";
$string = ;
close FILE;
}


#set as local undef in the undef or use $/="\n"; to reset the undef

1 comment:

Anonymous said...

If we continue coding after these steps, it will affect further...

Meaning: If we open one more file, it will retain the same functionality (making the file as single string). So avoid this, make it as local undef.

Or after closing the file, do the

$/="\n";