use strict; my @lines; # read the whole file into an array { # we use a block here so the lexical filehandle variable goes automatically out of scope and gets closed open my $filehandle, '<', 'path/to/my/file'; @lines = <$filehandle>; } # do something with the lines in @lines #now save the array to disk { # we use a block here so the lexical filehandle variable goes automatically out of scope and gets closed open my $filehandle, '>', 'path/to/my/file'; print $filehandle @lines; }