Perl – quick read text files scripts

These two Perl utility scripts are used to quickly read a text file into either a string or an array.

sub read_file_into_string {
  my $s = '';
  open FH, @_[0];
  for (<FH>) { $s .= $_; }
  close FH;
  return $s;
}
sub read_file_into_array {
  my @lines = ();
  open FH, @_[0];
  for (<FH>) {
    chomp;
    push @lines, $_;
  }
  close FH;
  return @lines;
}

Leave a Reply