my $slurp; { local $/ = undef; open my $textfile, '<', 'filename.txt' or die $!; $slurp = <$textfile>; close $textfile; } #### open my $textfile, '<', 'filename.txt' or die $!; while ( my $line = <$textfile> ) { ## Do something with $line }