use IO::Handle; my $infile= "input.txt"; my $outfile= "sorted.txt"; my $in= IO::Handle->new(); open $in, "< $infile\0" or die "Can't read $infile: $!\n"; my $out= IO::Handle->new(); open $out, "> $outfile\0" or die "Can't write $outfile: $!\n"; my @list= <$in>; close $in; # ... sort code here ... print $out @list;